github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/event/v2/metadata_test.go (about)

     1  /*
     2  Copyright 2021 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 v2
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  
    23  	"google.golang.org/protobuf/testing/protocmp"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    26  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    27  	"github.com/GoogleContainerTools/skaffold/testutil"
    28  )
    29  
    30  func TestEmptyState(t *testing.T) {
    31  	tests := []struct {
    32  		description string
    33  		cfg         latest.Pipeline
    34  		cluster     string
    35  		expected    *proto.Metadata
    36  	}{
    37  		{
    38  			description: "one build artifact minikube cluster multiple deployers",
    39  			cfg: latest.Pipeline{
    40  				Build: latest.BuildConfig{
    41  					BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}},
    42  					Artifacts: []*latest.Artifact{{ImageName: "docker-artifact-1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}}},
    43  				},
    44  				Deploy: latest.DeployConfig{
    45  					DeployType: latest.DeployType{
    46  						KubectlDeploy: &latest.KubectlDeploy{},
    47  						HelmDeploy:    &latest.HelmDeploy{Releases: []latest.HelmRelease{{Name: "first"}, {Name: "second"}}},
    48  					},
    49  				},
    50  			},
    51  			cluster: "minikube",
    52  			expected: &proto.Metadata{
    53  				Build: &proto.BuildMetadata{
    54  					Type:      proto.BuildType_LOCAL,
    55  					Artifacts: []*proto.BuildMetadata_Artifact{{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-1"}},
    56  				},
    57  				Render: &proto.RenderMetadata{},
    58  				Deploy: &proto.DeployMetadata{
    59  					Cluster: proto.ClusterType_MINIKUBE,
    60  					Deployers: []*proto.DeployMetadata_Deployer{
    61  						{Type: proto.DeployerType_HELM, Count: 2},
    62  						{Type: proto.DeployerType_KUBECTL, Count: 1},
    63  					},
    64  				},
    65  				RunID: "run-id",
    66  			},
    67  		},
    68  		{
    69  			description: "multiple artifacts of different types gke cluster 1 deployer ",
    70  			cfg: latest.Pipeline{
    71  				Build: latest.BuildConfig{
    72  					BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}},
    73  					Artifacts: []*latest.Artifact{
    74  						{ImageName: "docker-artifact-1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
    75  						{ImageName: "docker-artifact-2", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
    76  						{ImageName: "jib-artifact-1", ArtifactType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{}}},
    77  					},
    78  				},
    79  				Deploy: latest.DeployConfig{
    80  					DeployType: latest.DeployType{
    81  						KustomizeDeploy: &latest.KustomizeDeploy{},
    82  					},
    83  				},
    84  			},
    85  			cluster: "gke-tejal-test",
    86  			expected: &proto.Metadata{
    87  				Build: &proto.BuildMetadata{
    88  					Type: proto.BuildType_CLUSTER,
    89  					Artifacts: []*proto.BuildMetadata_Artifact{
    90  						{Type: proto.BuilderType_JIB, Name: "jib-artifact-1"},
    91  						{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-1"},
    92  						{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-2"},
    93  					},
    94  				},
    95  				Render: &proto.RenderMetadata{},
    96  				Deploy: &proto.DeployMetadata{
    97  					Cluster:   proto.ClusterType_GKE,
    98  					Deployers: []*proto.DeployMetadata_Deployer{{Type: proto.DeployerType_KUSTOMIZE, Count: 1}},
    99  				},
   100  				RunID: "run-id",
   101  			},
   102  		},
   103  		{
   104  			description: "no deployer, kaniko artifact, GCB build",
   105  			cfg: latest.Pipeline{
   106  				Build: latest.BuildConfig{
   107  					BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{}},
   108  					Artifacts: []*latest.Artifact{
   109  						{ImageName: "artifact-1", ArtifactType: latest.ArtifactType{KanikoArtifact: &latest.KanikoArtifact{}}},
   110  					},
   111  				},
   112  			},
   113  			cluster: "gke-tejal-test",
   114  			expected: &proto.Metadata{
   115  				Build: &proto.BuildMetadata{
   116  					Type:      proto.BuildType_GCB,
   117  					Artifacts: []*proto.BuildMetadata_Artifact{{Type: proto.BuilderType_KANIKO, Name: "artifact-1"}},
   118  				},
   119  				Render: &proto.RenderMetadata{},
   120  				Deploy: &proto.DeployMetadata{},
   121  				RunID:  "run-id",
   122  			},
   123  		},
   124  		{
   125  			description: "no build, kustomize deployer other cluster",
   126  			cfg: latest.Pipeline{
   127  				Deploy: latest.DeployConfig{
   128  					DeployType: latest.DeployType{
   129  						KustomizeDeploy: &latest.KustomizeDeploy{},
   130  					},
   131  				},
   132  			},
   133  			cluster: "some-private",
   134  			expected: &proto.Metadata{
   135  				Build:  &proto.BuildMetadata{},
   136  				Render: &proto.RenderMetadata{},
   137  				Deploy: &proto.DeployMetadata{
   138  					Cluster:   proto.ClusterType_OTHER,
   139  					Deployers: []*proto.DeployMetadata_Deployer{{Type: proto.DeployerType_KUSTOMIZE, Count: 1}},
   140  				},
   141  				RunID: "run-id",
   142  			},
   143  		},
   144  	}
   145  	for _, test := range tests {
   146  		testutil.Run(t, test.description, func(t *testutil.T) {
   147  			handler = &eventHandler{
   148  				state: emptyState(mockCfg([]latest.Pipeline{test.cfg}, test.cluster)),
   149  			}
   150  			metadata := handler.state.Metadata
   151  			artifacts := metadata.Build.Artifacts
   152  
   153  			// sort and compare
   154  			sort.Slice(artifacts, func(i, j int) bool { return artifacts[i].Type < artifacts[j].Type })
   155  			t.CheckDeepEqual(metadata, test.expected, protocmp.Transform())
   156  		})
   157  	}
   158  }