github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/event/v2/state_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  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	"google.golang.org/protobuf/testing/protocmp"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    27  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestGetState(t *testing.T) {
    32  	ev := newHandler()
    33  	ev.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))
    34  
    35  	ev.stateLock.Lock()
    36  	ev.state.BuildState.Artifacts["img"] = Complete
    37  	ev.stateLock.Unlock()
    38  
    39  	state := ev.getState()
    40  
    41  	testutil.CheckDeepEqual(t, Complete, state.BuildState.Artifacts["img"])
    42  }
    43  
    44  func TestResetStateOnBuild(t *testing.T) {
    45  	defer func() { handler = newHandler() }()
    46  	handler = newHandler()
    47  	handler.state = &proto.State{
    48  		BuildState: &proto.BuildState{
    49  			Artifacts: map[string]string{
    50  				"image1": Complete,
    51  			},
    52  		},
    53  		RenderState: &proto.RenderState{Status: Complete},
    54  		DeployState: &proto.DeployState{Status: Complete},
    55  		ForwardedPorts: map[int32]*proto.PortForwardEvent{
    56  			2001: {
    57  				LocalPort:  2000,
    58  				PodName:    "test/pod",
    59  				TargetPort: &targetPort,
    60  			},
    61  		},
    62  		StatusCheckState: &proto.StatusCheckState{Status: Complete},
    63  		FileSyncState:    &proto.FileSyncState{Status: Succeeded},
    64  	}
    65  
    66  	ResetStateOnBuild()
    67  	expected := &proto.State{
    68  		BuildState: &proto.BuildState{
    69  			Artifacts: map[string]string{
    70  				"image1": NotStarted,
    71  			},
    72  		},
    73  		TestState:        &proto.TestState{Status: NotStarted},
    74  		RenderState:      &proto.RenderState{Status: NotStarted},
    75  		DeployState:      &proto.DeployState{Status: NotStarted},
    76  		StatusCheckState: &proto.StatusCheckState{Status: NotStarted, Resources: map[string]string{}},
    77  		FileSyncState:    &proto.FileSyncState{Status: NotStarted},
    78  	}
    79  	testutil.CheckDeepEqual(t, expected, handler.getState(), cmpopts.EquateEmpty(), protocmp.Transform())
    80  }
    81  
    82  func TestResetStateOnDeploy(t *testing.T) {
    83  	defer func() { handler = newHandler() }()
    84  	handler = newHandler()
    85  	handler.state = &proto.State{
    86  		BuildState: &proto.BuildState{
    87  			Artifacts: map[string]string{
    88  				"image1": Complete,
    89  			},
    90  		},
    91  		DeployState: &proto.DeployState{Status: Complete},
    92  		ForwardedPorts: map[int32]*proto.PortForwardEvent{
    93  			2001: {
    94  				LocalPort:  2000,
    95  				PodName:    "test/pod",
    96  				TargetPort: &targetPort,
    97  			},
    98  		},
    99  		StatusCheckState: &proto.StatusCheckState{Status: Complete},
   100  	}
   101  	ResetStateOnDeploy()
   102  	expected := &proto.State{
   103  		BuildState: &proto.BuildState{
   104  			Artifacts: map[string]string{
   105  				"image1": Complete,
   106  			},
   107  		},
   108  		DeployState: &proto.DeployState{Status: NotStarted},
   109  		StatusCheckState: &proto.StatusCheckState{Status: NotStarted,
   110  			Resources: map[string]string{},
   111  		},
   112  	}
   113  	testutil.CheckDeepEqual(t, expected, handler.getState(), cmpopts.EquateEmpty(), protocmp.Transform())
   114  }
   115  
   116  func TestEmptyStateCheckState(t *testing.T) {
   117  	actual := emptyStatusCheckState()
   118  	expected := &proto.StatusCheckState{Status: NotStarted,
   119  		Resources: map[string]string{},
   120  	}
   121  	testutil.CheckDeepEqual(t, expected, actual, cmpopts.EquateEmpty(), protocmp.Transform())
   122  }
   123  
   124  func TestUpdateStateAutoTriggers(t *testing.T) {
   125  	defer func() { handler = newHandler() }()
   126  	handler = newHandler()
   127  	handler.state = &proto.State{
   128  		BuildState: &proto.BuildState{
   129  			Artifacts: map[string]string{
   130  				"image1": Complete,
   131  			},
   132  			AutoTrigger: false,
   133  		},
   134  		DeployState: &proto.DeployState{Status: Complete, AutoTrigger: false},
   135  		ForwardedPorts: map[int32]*proto.PortForwardEvent{
   136  			2001: {
   137  				LocalPort:  2000,
   138  				PodName:    "test/pod",
   139  				TargetPort: &targetPort,
   140  			},
   141  		},
   142  		StatusCheckState: &proto.StatusCheckState{Status: Complete},
   143  		FileSyncState: &proto.FileSyncState{
   144  			Status:      "Complete",
   145  			AutoTrigger: false,
   146  		},
   147  	}
   148  	UpdateStateAutoBuildTrigger(true)
   149  	UpdateStateAutoDeployTrigger(true)
   150  	UpdateStateAutoSyncTrigger(true)
   151  
   152  	expected := &proto.State{
   153  		BuildState: &proto.BuildState{
   154  			Artifacts: map[string]string{
   155  				"image1": Complete,
   156  			},
   157  			AutoTrigger: true,
   158  		},
   159  		DeployState: &proto.DeployState{Status: Complete, AutoTrigger: true},
   160  		ForwardedPorts: map[int32]*proto.PortForwardEvent{
   161  			2001: {
   162  				LocalPort:  2000,
   163  				PodName:    "test/pod",
   164  				TargetPort: &targetPort,
   165  			},
   166  		},
   167  		StatusCheckState: &proto.StatusCheckState{Status: Complete},
   168  		FileSyncState: &proto.FileSyncState{
   169  			Status:      "Complete",
   170  			AutoTrigger: true,
   171  		},
   172  	}
   173  	testutil.CheckDeepEqual(t, expected, handler.getState(), cmpopts.EquateEmpty(), protocmp.Transform())
   174  }
   175  
   176  func TestAutoTriggerDiff(t *testing.T) {
   177  	tests := []struct {
   178  		description  string
   179  		phase        constants.Phase
   180  		handlerState *proto.State
   181  		val          bool
   182  		expected     bool
   183  	}{
   184  		{
   185  			description: "build needs update",
   186  			phase:       constants.Build,
   187  			val:         true,
   188  			handlerState: &proto.State{
   189  				BuildState: &proto.BuildState{
   190  					AutoTrigger: false,
   191  				},
   192  			},
   193  			expected: true,
   194  		},
   195  		{
   196  			description: "deploy doesn't need update",
   197  			phase:       constants.Deploy,
   198  			val:         true,
   199  			handlerState: &proto.State{
   200  				BuildState: &proto.BuildState{
   201  					AutoTrigger: false,
   202  				},
   203  				DeployState: &proto.DeployState{
   204  					AutoTrigger: true,
   205  				},
   206  			},
   207  			expected: false,
   208  		},
   209  		{
   210  			description: "sync needs update",
   211  			phase:       constants.Sync,
   212  			val:         false,
   213  			handlerState: &proto.State{
   214  				FileSyncState: &proto.FileSyncState{
   215  					AutoTrigger: true,
   216  				},
   217  			},
   218  			expected: true,
   219  		},
   220  	}
   221  
   222  	for _, test := range tests {
   223  		testutil.Run(t, test.description, func(t *testutil.T) {
   224  			// Setup handler state
   225  			handler.setState(test.handlerState)
   226  
   227  			got, err := AutoTriggerDiff(test.phase, test.val)
   228  			if err != nil {
   229  				t.Fail()
   230  			}
   231  
   232  			t.CheckDeepEqual(test.expected, got)
   233  		})
   234  	}
   235  }