package kube import ( "io" "strings" "testing" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/client/unversioned/fake" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" ) func TestPerform(t *testing.T) { tests := []struct { name string namespace string reader io.Reader count int err bool errMessage string }{ { name: "Valid input", namespace: "test", reader: strings.NewReader(guestbookManifest), count: 6, }, { name: "Empty manifests", namespace: "test", reader: strings.NewReader(""), err: true, errMessage: "no objects passed to create", }, } for _, tt := range tests { results := []*resource.Info{} fn := func(info *resource.Info) error { results = append(results, info) if info.Namespace != tt.namespace { t.Errorf("%q. expected namespace to be '%s', got %s", tt.name, tt.namespace, info.Namespace) } return nil } f := cmdutil.NewFactory(nil) f.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) { return &fake.RESTClient{}, nil } err := perform(f, tt.namespace, tt.reader, fn) if (err != nil) != tt.err { t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err) } if err != nil && err.Error() != tt.errMessage { t.Errorf("%q. expected error message: %v, got %v", tt.name, tt.errMessage, err) } if len(results) != tt.count { t.Errorf("%q. expected %d result objects, got %d", tt.name, tt.count, len(results)) } } } func TestReal(t *testing.T) { t.Skip("This is a live test, comment this line to run") if err := New(nil).Create("test", strings.NewReader(guestbookManifest)); err != nil { t.Fatal(err) } if err := New(nil).Delete("test", strings.NewReader(guestbookManifest)); err != nil { t.Fatal(err) } } const guestbookManifest = ` apiVersion: v1 kind: Service metadata: name: redis-master labels: app: redis tier: backend role: master spec: ports: - port: 6379 targetPort: 6379 selector: app: redis tier: backend role: master --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: redis-master spec: replicas: 1 template: metadata: labels: app: redis role: master tier: backend spec: containers: - name: master image: gcr.io/google_containers/redis:e2e # or just image: redis resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 6379 --- apiVersion: v1 kind: Service metadata: name: redis-slave labels: app: redis tier: backend role: slave spec: ports: # the port that this service should serve on - port: 6379 selector: app: redis tier: backend role: slave --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: redis-slave spec: replicas: 2 template: metadata: labels: app: redis role: slave tier: backend spec: containers: - name: slave image: gcr.io/google_samples/gb-redisslave:v1 resources: requests: cpu: 100m memory: 100Mi env: - name: GET_HOSTS_FROM value: dns ports: - containerPort: 6379 --- apiVersion: v1 kind: Service metadata: name: frontend labels: app: guestbook tier: frontend spec: ports: - port: 80 selector: app: guestbook tier: frontend --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: frontend spec: replicas: 3 template: metadata: labels: app: guestbook tier: frontend spec: containers: - name: php-redis image: gcr.io/google-samples/gb-frontend:v4 resources: requests: cpu: 100m memory: 100Mi env: - name: GET_HOSTS_FROM value: dns ports: - containerPort: 80 `