github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/delete_test.go (about)

     1  /*
     2  Copyright 2022 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 integration
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/docker/docker/api/types"
    25  	"github.com/docker/docker/api/types/filters"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    28  	"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
    29  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    30  )
    31  
    32  func TestDelete(t *testing.T) {
    33  	var tests = []struct {
    34  		description string
    35  		dir         string
    36  		args        []string
    37  		pods        []string
    38  		deployments []string
    39  		env         []string
    40  	}{
    41  		{
    42  			description: "getting-started",
    43  			dir:         "testdata/getting-started",
    44  			pods:        []string{"getting-started"},
    45  		},
    46  		{
    47  			description: "templated fields must exist",
    48  			dir:         "testdata/helm-render-delete",
    49  		},
    50  		{
    51  			description: "microservices",
    52  			dir:         "examples/microservices",
    53  			args:        []string{"--status-check=false"},
    54  			deployments: []string{"leeroy-app", "leeroy-web"},
    55  		},
    56  		{
    57  			description: "multi-config-microservices",
    58  			dir:         "examples/multi-config-microservices",
    59  			deployments: []string{"leeroy-app", "leeroy-web"},
    60  		},
    61  		{
    62  			description: "multiple deployers",
    63  			dir:         "testdata/deploy-multiple",
    64  			pods:        []string{"deploy-kubectl", "deploy-kustomize"},
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run(test.description, func(t *testing.T) {
    70  			MarkIntegrationTest(t, CanRunWithoutGcp)
    71  			ns, client := SetupNamespace(t)
    72  
    73  			args := append(test.args, "--cache-artifacts=false")
    74  			skaffold.Run(args...).InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunOrFail(t)
    75  
    76  			client.WaitForPodsReady(test.pods...)
    77  			client.waitForDeploymentsToStabilizeWithTimeout(time.Minute*2, test.deployments...)
    78  
    79  			skaffold.Delete().InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunOrFail(t)
    80  		})
    81  	}
    82  }
    83  
    84  func TestDeleteDockerDeployer(t *testing.T) {
    85  	tests := []struct {
    86  		description        string
    87  		dir                string
    88  		args               []string
    89  		deployedContainers []string
    90  	}{
    91  		{
    92  			description:        "run with one container",
    93  			dir:                "testdata/docker-deploy",
    94  			args:               []string{"-p", "one-container"},
    95  			deployedContainers: []string{"docker-bert-img-1"},
    96  		},
    97  		{
    98  			description:        "run with more than one container",
    99  			dir:                "testdata/docker-deploy",
   100  			args:               []string{"-p", "more-than-one-container"},
   101  			deployedContainers: []string{"docker-bert-img-2", "docker-ernie-img-2"},
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		testutil.Run(t, test.description, func(t *testutil.T) {
   107  			MarkIntegrationTest(t.T, CanRunWithoutGcp)
   108  			ctx := context.Background()
   109  			skaffold.Run(test.args...).InDir(test.dir).RunOrFail(t.T)
   110  			skaffold.Delete(test.args...).InDir(test.dir).RunOrFail(t.T)
   111  
   112  			client := SetupDockerClient(t.T)
   113  			cs := getContainers(ctx, t, test.deployedContainers, client)
   114  			t.CheckDeepEqual(0, len(cs))
   115  		})
   116  	}
   117  }
   118  
   119  func getContainers(ctx context.Context, t *testutil.T, deployedContainers []string, client docker.LocalDaemon) []types.Container {
   120  	t.Helper()
   121  
   122  	containersFilters := []filters.KeyValuePair{}
   123  	for _, c := range deployedContainers {
   124  		containersFilters = append(containersFilters, filters.Arg("name", c))
   125  	}
   126  
   127  	cl, err := client.ContainerList(ctx, types.ContainerListOptions{
   128  		Filters: filters.NewArgs(containersFilters...),
   129  	})
   130  	t.CheckNoError(err)
   131  
   132  	return cl
   133  }
   134  
   135  func TestDeleteNonExistedHelmResource(t *testing.T) {
   136  	var tests = []struct {
   137  		description string
   138  		dir         string
   139  		env         []string
   140  	}{
   141  		{
   142  			description: "helm deployment doesn't exist.",
   143  			dir:         "testdata/helm",
   144  			env:         []string{"TEST_NS=test-ns"},
   145  		},
   146  	}
   147  
   148  	for _, test := range tests {
   149  		t.Run(test.description, func(t *testing.T) {
   150  			MarkIntegrationTest(t, CanRunWithoutGcp)
   151  			ns, _ := SetupNamespace(t)
   152  
   153  			skaffold.Delete().InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunOrFail(t)
   154  		})
   155  	}
   156  }