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

     1  /*
     2  Copyright 2020 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  	"strings"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/v2/cmd/skaffold/app/flags"
    24  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    25  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    26  )
    27  
    28  func TestBuildAndTest(t *testing.T) {
    29  	MarkIntegrationTest(t, CanRunWithoutGcp)
    30  
    31  	ns, _ := SetupNamespace(t)
    32  
    33  	outputBytes := skaffold.Build("--quiet").InDir("examples/structure-tests").InNs(ns.Name).RunOrFailOutput(t)
    34  	// Parse the Build Output
    35  	buildArtifacts, err := flags.ParseBuildOutput(outputBytes)
    36  	failNowIfError(t, err)
    37  	if len(buildArtifacts.Builds) != 1 {
    38  		t.Fatalf("expected 1 artifacts to be built, but found %d", len(buildArtifacts.Builds))
    39  	}
    40  
    41  	var skaffoldExampleTag string
    42  	for _, a := range buildArtifacts.Builds {
    43  		if a.ImageName == "skaffold-example" {
    44  			skaffoldExampleTag = a.Tag
    45  		}
    46  	}
    47  	if skaffoldExampleTag == "" {
    48  		t.Fatalf("expected to find a tag for skaffold-example but found %s", skaffoldExampleTag)
    49  	}
    50  
    51  	tmpDir := testutil.NewTempDir(t)
    52  	buildOutputFile := tmpDir.Path("build.out")
    53  	tmpDir.Write("build.out", string(outputBytes))
    54  
    55  	// Run Test using the build output
    56  	skaffold.Test("--build-artifacts", buildOutputFile).InDir("examples/structure-tests").InNs(ns.Name).RunOrFail(t)
    57  }
    58  
    59  func TestTestWithInCorrectConfig(t *testing.T) {
    60  	MarkIntegrationTest(t, CanRunWithoutGcp)
    61  
    62  	ns, _ := SetupNamespace(t)
    63  
    64  	// We're not providing a tag for the getting-started image
    65  	output, err := skaffold.Test().InDir("examples/getting-started").InNs(ns.Name).RunWithCombinedOutput(t)
    66  
    67  	if err == nil {
    68  		t.Errorf("expected to see an error since not every image tag is provided: %s", output)
    69  	} else if !strings.Contains(string(output), "no tag provided for image [skaffold-example]") {
    70  		t.Errorf("failed without saying the reason: %s", output)
    71  	}
    72  }