github.com/splunk/dan1-qbec@v0.7.3/internal/commands/wait_test.go (about)

     1  /*
     2     Copyright 2019 Splunk Inc.
     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 commands
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/splunk/qbec/internal/model"
    25  	"github.com/splunk/qbec/internal/sio"
    26  	"github.com/splunk/qbec/internal/types"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func testDisplayName(obj model.K8sMeta) string {
    31  	return fmt.Sprintf("%s/%s %s/%s", obj.GroupVersionKind().Group, obj.GroupVersionKind().Kind, obj.GetNamespace(), obj.GetName())
    32  }
    33  
    34  func testDeployment(name string) model.K8sMeta {
    35  	return model.NewK8sObject(map[string]interface{}{
    36  		"apiVersion": "apps/v1",
    37  		"kind":       "Deployment",
    38  		"metadata": map[string]interface{}{
    39  			"namespace": "test-ns",
    40  			"name":      name,
    41  		},
    42  	})
    43  }
    44  
    45  func TestWaitListener(t *testing.T) {
    46  	var buf bytes.Buffer
    47  	oldOutput, oldColors := sio.Output, sio.EnableColors
    48  	defer func() {
    49  		sio.Output = oldOutput
    50  		sio.EnableColors = oldColors
    51  	}()
    52  	sio.Output = &buf
    53  	sio.EnableColors = false
    54  
    55  	d1, d2, d3 := testDeployment("d1"), testDeployment("d2"), testDeployment("d3")
    56  	wl := &waitListener{displayNameFn: testDisplayName}
    57  	wl.OnInit([]model.K8sMeta{d1, d2, d3})
    58  	wl.OnStatusChange(d1, types.RolloutStatus{Description: "starting d1 rollout"})
    59  	wl.OnStatusChange(d2, types.RolloutStatus{Description: "1 of 2 replicas updated"})
    60  	wl.OnStatusChange(d3, types.RolloutStatus{Description: "waiting for version"})
    61  	wl.OnStatusChange(d1, types.RolloutStatus{Description: "successful rollout", Done: true})
    62  	wl.OnStatusChange(d2, types.RolloutStatus{Description: "successful rollout", Done: true})
    63  	wl.OnStatusChange(d3, types.RolloutStatus{Description: "successful rollout", Done: true})
    64  
    65  	wl.OnEnd(nil)
    66  
    67  	output := buf.String()
    68  	a := assert.New(t)
    69  	a.Contains(output, "waiting for readiness of 3 objects")
    70  	a.Contains(output, "- apps/Deployment test-ns/d1")
    71  	a.Contains(output, "0s    : apps/Deployment test-ns/d1 :: starting d1 rollout")
    72  	a.Contains(output, "✓ 0s    : apps/Deployment test-ns/d1 :: successful rollout (2 remaining)")
    73  	a.Contains(output, "rollout complete")
    74  }
    75  
    76  func TestWaitListenerTimeout(t *testing.T) {
    77  	var buf bytes.Buffer
    78  	oldOutput, oldColors := sio.Output, sio.EnableColors
    79  	defer func() {
    80  		sio.Output = oldOutput
    81  		sio.EnableColors = oldColors
    82  	}()
    83  	sio.Output = &buf
    84  	sio.EnableColors = false
    85  
    86  	d1, d2, d3 := testDeployment("d1"), testDeployment("d2"), testDeployment("d3")
    87  	wl := &waitListener{displayNameFn: testDisplayName}
    88  	wl.OnInit([]model.K8sMeta{d1, d2, d3})
    89  	wl.OnStatusChange(d1, types.RolloutStatus{Description: "starting d1 rollout"})
    90  	wl.OnStatusChange(d2, types.RolloutStatus{Description: "1 of 2 replicas updated"})
    91  	wl.OnStatusChange(d3, types.RolloutStatus{Description: "waiting for version"})
    92  	wl.OnStatusChange(d1, types.RolloutStatus{Description: "successful rollout", Done: true})
    93  	wl.OnStatusChange(d2, types.RolloutStatus{Description: "successful rollout", Done: true})
    94  	wl.OnError(d3, fmt.Errorf("d3 missing"))
    95  
    96  	wl.OnEnd(fmt.Errorf("1 error"))
    97  
    98  	output := buf.String()
    99  	a := assert.New(t)
   100  	a.Contains(output, "waiting for readiness of 3 objects")
   101  	a.Contains(output, "- apps/Deployment test-ns/d1")
   102  	a.Contains(output, "0s    : apps/Deployment test-ns/d1 :: starting d1 rollout")
   103  	a.Contains(output, "✓ 0s    : apps/Deployment test-ns/d1 :: successful rollout (2 remaining)")
   104  	a.Contains(output, "✘ 0s    : apps/Deployment test-ns/d3 :: d3 missing")
   105  	a.Contains(output, "rollout not complete for the following 1 object")
   106  }
   107  
   108  func TestWaitWatcher(t *testing.T) {
   109  
   110  }