github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/deploy/kubectl_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 deploy
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  func TestGenerateKubectlPipeline(t *testing.T) {
    28  	tmpDir := testutil.NewTempDir(t)
    29  
    30  	tmpDir.Write("deployment.yaml", `apiVersion: v1
    31  kind: Pod
    32  metadata:
    33    name: getting-started
    34  spec:
    35    containers:
    36    - name: getting-started
    37      image: gcr.io/k8s-skaffold/skaffold-example
    38  `)
    39  	filename := tmpDir.Path("deployment.yaml")
    40  
    41  	k := newKubectlInitializer([]string{filename})
    42  
    43  	expectedConfig := latest.DeployConfig{
    44  		DeployType: latest.DeployType{
    45  			KubectlDeploy: &latest.KubectlDeploy{
    46  				Manifests: []string{filename},
    47  			},
    48  		},
    49  	}
    50  	deployConfig, profiles := k.DeployConfig()
    51  	testutil.CheckDeepEqual(t, expectedConfig, deployConfig)
    52  	if profiles != nil {
    53  		t.Errorf("generated profiles should be nil, but got: %+v\n", profiles)
    54  	}
    55  }
    56  
    57  func TestParseImagesFromKubernetesYaml(t *testing.T) {
    58  	tests := []struct {
    59  		description string
    60  		contents    string
    61  		images      []string
    62  		shouldErr   bool
    63  	}{
    64  		{
    65  			description: "incorrect k8s yaml",
    66  			contents: `no apiVersion: t
    67  kind: Pod`,
    68  			images:    nil,
    69  			shouldErr: true,
    70  		},
    71  		{
    72  			description: "correct k8s yaml",
    73  			contents: `apiVersion: v1
    74  kind: Pod
    75  metadata:
    76    name: getting-started
    77  spec:
    78    containers:
    79    - name: getting-started
    80      image: gcr.io/k8s-skaffold/skaffold-example`,
    81  			images:    []string{"gcr.io/k8s-skaffold/skaffold-example"},
    82  			shouldErr: false,
    83  		},
    84  		{
    85  			description: "correct rolebinding yaml with no image",
    86  			contents: `apiVersion: rbac.authorization.k8s.io/v1
    87  kind: RoleBinding
    88  metadata:
    89    name: default-admin
    90    namespace: default
    91  roleRef:
    92    apiGroup: rbac.authorization.k8s.io
    93    kind: ClusterRole
    94    name: admin
    95  subjects:
    96  - name: default
    97    kind: ServiceAccount
    98    namespace: default`,
    99  			images:    nil,
   100  			shouldErr: false,
   101  		},
   102  		{
   103  			description: "crd",
   104  			contents: `apiVersion: my.crd.io/v1
   105  kind: CustomType
   106  metadata:
   107    name: test crd
   108  spec:
   109    containers:
   110    - name: container
   111      image: gcr.io/my/image`,
   112  			images:    []string{"gcr.io/my/image"},
   113  			shouldErr: false,
   114  		},
   115  	}
   116  	for _, test := range tests {
   117  		testutil.Run(t, test.description, func(t *testutil.T) {
   118  			tmpDir := t.NewTempDir().
   119  				Write("deployment.yaml", test.contents)
   120  
   121  			images, err := kubernetes.ParseImagesFromKubernetesYaml(tmpDir.Path("deployment.yaml"))
   122  
   123  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.images, images)
   124  		})
   125  	}
   126  }
   127  
   128  func TestIsKubernetesManifest(t *testing.T) {
   129  	tests := []struct {
   130  		description string
   131  		filename    string
   132  		content     string
   133  		expected    bool
   134  	}{
   135  		{
   136  			description: "valid k8s yaml filename format",
   137  			filename:    "test1.yaml",
   138  			content:     "apiVersion: v1\nkind: Service\nmetadata:\n  name: test\n",
   139  			expected:    true,
   140  		},
   141  		{
   142  			description: "valid k8s json filename format",
   143  			filename:    "test1.json",
   144  			content:     `{"apiVersion":"v1","kind":"Service","metadata":{"name": "test"}}`,
   145  			expected:    true,
   146  		},
   147  		{
   148  			description: "valid k8s yaml filename format",
   149  			filename:    "test1.yml",
   150  			content:     "apiVersion: v1\nkind: Service\nmetadata:\n  name: test\n",
   151  			expected:    true,
   152  		},
   153  		{
   154  			description: "invalid k8s yaml",
   155  			filename:    "test1.yaml",
   156  			content:     "key: value",
   157  			expected:    false,
   158  		},
   159  		{
   160  			description: "invalid k8s json",
   161  			filename:    "test1.json",
   162  			content:     `{}`,
   163  			expected:    false,
   164  		},
   165  		{
   166  			description: "invalid k8s yml",
   167  			filename:    "test1.yml",
   168  			content:     "key: value",
   169  			expected:    false,
   170  		},
   171  		{
   172  			description: "invalid file",
   173  			filename:    "some.config",
   174  			content:     "",
   175  			expected:    false,
   176  		},
   177  	}
   178  	for _, test := range tests {
   179  		testutil.Run(t, test.description, func(t *testutil.T) {
   180  			t.NewTempDir().Write(test.filename, test.content).Chdir()
   181  
   182  			supported := kubernetes.IsKubernetesManifest(test.filename)
   183  
   184  			t.CheckDeepEqual(test.expected, supported)
   185  		})
   186  	}
   187  }