github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/server/v2/server_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  	"context"
    21  	"testing"
    22  
    23  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  // Mock structs and functions
    28  type mockData struct {
    29  	Build  bool
    30  	Sync   bool
    31  	Deploy bool
    32  }
    33  
    34  var data mockData
    35  var done = make(chan bool)
    36  
    37  func mockBuildIntentCallback() {
    38  	data.Build = true
    39  	done <- true
    40  }
    41  func mockSyncIntentCallback() {
    42  	data.Sync = true
    43  	done <- true
    44  }
    45  func mockDeployIntentCallback() {
    46  	data.Deploy = true
    47  	done <- true
    48  }
    49  
    50  func TestServer_Execute(t *testing.T) {
    51  	tests := []struct {
    52  		description  string
    53  		request      *proto.UserIntentRequest
    54  		numCallBacks int
    55  		expected     mockData
    56  	}{
    57  		{
    58  			description: "build intent",
    59  			request: &proto.UserIntentRequest{
    60  				Intent: &proto.Intent{
    61  					Build: true,
    62  				},
    63  			},
    64  			numCallBacks: 1,
    65  			expected: mockData{
    66  				Build: true,
    67  			},
    68  		},
    69  		{
    70  			description: "sync intent",
    71  			request: &proto.UserIntentRequest{
    72  				Intent: &proto.Intent{
    73  					Sync: true,
    74  				},
    75  			},
    76  			numCallBacks: 1,
    77  			expected: mockData{
    78  				Sync: true,
    79  			},
    80  		},
    81  		{
    82  			description: "deploy intent",
    83  			request: &proto.UserIntentRequest{
    84  				Intent: &proto.Intent{
    85  					Deploy: true,
    86  				},
    87  			},
    88  			numCallBacks: 1,
    89  			expected: mockData{
    90  				Deploy: true,
    91  			},
    92  		},
    93  		{
    94  			description: "build and deploy intent",
    95  			request: &proto.UserIntentRequest{
    96  				Intent: &proto.Intent{
    97  					Build:  true,
    98  					Deploy: true,
    99  				},
   100  			},
   101  			numCallBacks: 2,
   102  			expected: mockData{
   103  				Build:  true,
   104  				Deploy: true,
   105  			},
   106  		},
   107  		{
   108  			description: "build, sync, and deploy intent",
   109  			request: &proto.UserIntentRequest{
   110  				Intent: &proto.Intent{
   111  					Build:  true,
   112  					Deploy: true,
   113  					Sync:   true,
   114  				},
   115  			},
   116  			numCallBacks: 3,
   117  			expected: mockData{
   118  				Build:  true,
   119  				Sync:   true,
   120  				Deploy: true,
   121  			},
   122  		},
   123  		{
   124  			description: "devloop intent",
   125  			request: &proto.UserIntentRequest{
   126  				Intent: &proto.Intent{
   127  					Devloop: true,
   128  				},
   129  			},
   130  			numCallBacks: 1,
   131  			expected: mockData{
   132  				Build: true,
   133  			},
   134  		},
   135  	}
   136  
   137  	for _, test := range tests {
   138  		testutil.Run(t, test.description, func(t *testutil.T) {
   139  			t.Override(&resetStateOnBuild, func() {})
   140  			t.Override(&resetStateOnDeploy, func() {})
   141  			data = mockData{}
   142  
   143  			// Setup server with mock callback functions and run Execute()
   144  			Srv = &Server{
   145  				BuildIntentCallback:  mockBuildIntentCallback,
   146  				SyncIntentCallback:   mockSyncIntentCallback,
   147  				DeployIntentCallback: mockDeployIntentCallback,
   148  			}
   149  			_, err := Srv.Execute(context.Background(), test.request)
   150  			if err != nil {
   151  				t.Fail()
   152  			}
   153  
   154  			// Ensure callbacks finish updating data
   155  			for i := 0; i < test.numCallBacks; i++ {
   156  				<-done
   157  			}
   158  
   159  			t.CheckDeepEqual(test.expected, data)
   160  		})
   161  	}
   162  }