github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/manifest/namespace_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package manifest
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/testutil"
    23  )
    24  
    25  func TestCollectNamespaces(t *testing.T) {
    26  	tests := []struct {
    27  		description string
    28  		manifests   ManifestList
    29  		expected    []string
    30  	}{
    31  		{
    32  			description: "single Pod manifest in the list",
    33  			manifests: ManifestList{[]byte(`
    34  apiVersion: v1
    35  kind: Pod
    36  metadata:
    37    name: getting-started
    38    namespace: test
    39  spec:
    40    containers:
    41    - image: gcr.io/k8s-skaffold/example
    42      name: example
    43  `)},
    44  			expected: []string{"test"},
    45  		}, {
    46  			description: "single Service manifest in the list",
    47  			manifests: ManifestList{[]byte(`
    48  apiVersion: v1
    49  kind: Service
    50  metadata:
    51    name: getting-started
    52    namespace: test
    53  spec:
    54    type: ClusterIP
    55    ports:
    56    - port: 443
    57      targetPort: 8443
    58      protocol: TCP
    59    selector:
    60      app: getting-started
    61  `)},
    62  			expected: []string{"test"},
    63  		}, {
    64  			description: "multiple manifest in the list with different namespaces",
    65  			manifests: ManifestList{[]byte(`
    66  apiVersion: v1
    67  kind: Pod
    68  metadata:
    69    name: foo
    70    namespace: test-foo
    71  spec:
    72    containers:
    73    - image: gcr.io/k8s-skaffold/example
    74      name: example`), []byte(`
    75  apiVersion: v1
    76  kind: Pod
    77  metadata:
    78    name: bar
    79    namespace: test-bar
    80  spec:
    81    containers:
    82    - image: gcr.io/k8s-skaffold/example
    83      name: example
    84  `)},
    85  			expected: []string{"test-bar", "test-foo"},
    86  		}, {
    87  			description: "multiple manifest but same namespace",
    88  			manifests: ManifestList{[]byte(`
    89  apiVersion: v1
    90  kind: Pod
    91  metadata:
    92    name: foo
    93    namespace: test
    94  spec:
    95    containers:
    96    - image: gcr.io/k8s-skaffold/example
    97      name: example`), []byte(`
    98  apiVersion: v1
    99  kind: Pod
   100  metadata:
   101    name: bar
   102    namespace: test
   103  spec:
   104    containers:
   105    - image: gcr.io/k8s-skaffold/example
   106      name: example
   107  `)},
   108  			expected: []string{"test"},
   109  		}, {
   110  			description: "multiple manifest but no namespace",
   111  			manifests: ManifestList{[]byte(`
   112  apiVersion: v1
   113  kind: Pod
   114  metadata:
   115    name: foo
   116  spec:
   117    containers:
   118    - image: gcr.io/k8s-skaffold/example
   119      name: example`), []byte(`
   120  apiVersion: v1
   121  kind: Pod
   122  metadata:
   123    name: bar
   124  spec:
   125    containers:
   126    - image: gcr.io/k8s-skaffold/example
   127      name: example
   128  `)},
   129  			expected: []string{},
   130  		}, {
   131  			description: "single Pod manifest with nil namespace",
   132  			manifests: ManifestList{[]byte(`
   133  apiVersion: v1
   134  kind: Pod
   135  metadata:
   136    name: getting-started
   137    namespace:
   138  spec:
   139    containers:
   140    - image: gcr.io/k8s-skaffold/example
   141      name: example
   142  `)},
   143  			expected: []string{},
   144  		}, {
   145  			description: "single Pod manifest with empty namespace",
   146  			manifests: ManifestList{[]byte(`
   147  apiVersion: v1
   148  kind: Pod
   149  metadata:
   150    name: getting-started
   151    namespace: ""
   152  spec:
   153    containers:
   154    - image: gcr.io/k8s-skaffold/example
   155      name: example
   156  `)},
   157  			expected: []string{},
   158  		}, {
   159  			description: "empty manifest",
   160  			manifests:   ManifestList{[]byte(``)},
   161  			expected:    []string{},
   162  		}, {
   163  			description: "unexpected metadata type",
   164  			manifests:   ManifestList{[]byte(`metadata: []`)},
   165  			expected:    []string{},
   166  		},
   167  	}
   168  	for _, test := range tests {
   169  		testutil.Run(t, test.description, func(t *testutil.T) {
   170  			actual, err := test.manifests.CollectNamespaces()
   171  			t.CheckNoError(err)
   172  			t.CheckDeepEqual(test.expected, actual)
   173  		})
   174  	}
   175  }