github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/test_events_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  	"testing"
    21  	"time"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    24  	"github.com/GoogleContainerTools/skaffold/v2/proto/v1"
    25  )
    26  
    27  func TestTestEvents(t *testing.T) {
    28  	tests := []struct {
    29  		description string
    30  		podName     string
    31  		testDir     string
    32  		config      string
    33  		args        []string
    34  		numOfTests  int
    35  	}{
    36  		{
    37  			description: "test events for custom test",
    38  			podName:     "test-events",
    39  			testDir:     "testdata/test-events",
    40  			config:      "skaffold.yaml",
    41  			args:        []string{"--profile", "custom"},
    42  			numOfTests:  1,
    43  		},
    44  		{
    45  			description: "test events for structure test",
    46  			podName:     "test-events",
    47  			testDir:     "testdata/test-events",
    48  			config:      "skaffold.yaml",
    49  			args:        []string{"--profile", "structure"},
    50  			numOfTests:  1,
    51  		},
    52  		{
    53  			description: "test events for custom & structure tests",
    54  			podName:     "test-events",
    55  			testDir:     "testdata/test-events",
    56  			config:      "skaffold.yaml",
    57  			args:        []string{"--profile", "customandstructure"},
    58  			numOfTests:  2,
    59  		},
    60  	}
    61  	for _, test := range tests {
    62  		t.Run(test.description, func(t *testing.T) {
    63  			MarkIntegrationTest(t, CanRunWithoutGcp)
    64  
    65  			// Run skaffold build first to fail quickly on a build failure
    66  			skaffold.Build(test.args...).InDir(test.testDir).WithConfig(test.config).RunOrFail(t)
    67  
    68  			ns, client := SetupNamespace(t)
    69  			rpcAddr := randomPort()
    70  
    71  			// test.args...
    72  			args := append(test.args, "--rpc-port", rpcAddr)
    73  			skaffold.Dev(args...).InDir(test.testDir).WithConfig(test.config).InNs(ns.Name).RunLive(t)
    74  
    75  			client.WaitForPodsReady(test.podName)
    76  
    77  			// Ensure we see a test is triggered in the event log
    78  			_, entries := apiEvents(t, rpcAddr)
    79  
    80  			for i := 0; i < test.numOfTests; i++ {
    81  				verifyTestCompletedWithEvents(t, entries)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func verifyTestCompletedWithEvents(t *testing.T, entries chan *proto.LogEntry) {
    88  	// Ensure we see a test in progress triggered in the event log
    89  	err := waitForEvent(2*time.Minute, entries, func(e *proto.LogEntry) bool {
    90  		event := e.GetEvent().GetTestEvent()
    91  		return event != nil && event.GetStatus() == InProgress
    92  	})
    93  	failNowIfError(t, err)
    94  
    95  	// Ensure we see the test completed triggered in the event log
    96  	err = waitForEvent(2*time.Minute, entries, func(e *proto.LogEntry) bool {
    97  		event := e.GetEvent().GetTestEvent()
    98  		return event != nil && event.GetStatus() == "Complete"
    99  	})
   100  	failNowIfError(t, err)
   101  }