github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/custom_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 integration
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    27  	event "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/event/v2"
    28  	"github.com/GoogleContainerTools/skaffold/v2/proto/v1"
    29  )
    30  
    31  func TestCustomTest(t *testing.T) {
    32  	MarkIntegrationTest(t, CanRunWithoutGcp)
    33  
    34  	config := "skaffold.yaml"
    35  	expectedText := "bar\nbar\n"
    36  	testDir := "testdata/custom-test"
    37  	testFile := "testdata/custom-test/test"
    38  	depFile := "testdata/custom-test/testdep"
    39  	defer func() {
    40  		os.Truncate(depFile, 0)
    41  		os.Truncate(testFile, 0)
    42  	}()
    43  
    44  	// Run skaffold build first to fail quickly on a build failure
    45  	skaffold.Build().InDir(testDir).WithConfig(config).RunOrFail(t)
    46  
    47  	ns, client := SetupNamespace(t)
    48  
    49  	rpcAddr := randomPort()
    50  	skaffold.Dev("--rpc-port", rpcAddr).InDir(testDir).WithConfig(config).InNs(ns.Name).RunBackground(t)
    51  
    52  	_, entries := apiEvents(t, rpcAddr)
    53  
    54  	// Wait for the first devloop to register target files to the monitor before running command to change target files
    55  	failNowIfError(t, waitForEvent(90*time.Second, entries, func(e *proto.LogEntry) bool {
    56  		dle, ok := e.Event.EventType.(*proto.Event_DevLoopEvent)
    57  		return ok && dle.DevLoopEvent.Status == event.Succeeded
    58  	}))
    59  
    60  	client.WaitForPodsReady("custom-test-example")
    61  	os.WriteFile(depFile, []byte("foo"), 0644)
    62  
    63  	err := wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
    64  		out, e := os.ReadFile(testFile)
    65  		failNowIfError(t, e)
    66  		return string(out) == expectedText, nil
    67  	})
    68  	failNowIfError(t, err)
    69  }