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

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/splunk/qbec/internal/model"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    16  )
    17  
    18  func TestObjectStatus(t *testing.T) {
    19  	os := RolloutStatus{}
    20  	os.withDesc("foo").withDone(true)
    21  	assert.Equal(t, "foo", os.Description)
    22  	assert.True(t, os.Done)
    23  }
    24  
    25  func load(t *testing.T, file string) *unstructured.Unstructured {
    26  	b, err := ioutil.ReadFile(file)
    27  	require.Nil(t, err)
    28  	var data map[string]interface{}
    29  	err = json.Unmarshal(b, &data)
    30  	require.Nil(t, err)
    31  	return &unstructured.Unstructured{Object: data}
    32  }
    33  
    34  func checkExpectedStatus(t *testing.T, data *unstructured.Unstructured, rev int64, fn RolloutStatusFunc) {
    35  	a := assert.New(t)
    36  	expectedDesc := data.GetAnnotations()["test/status"]
    37  	expectedDone := data.GetAnnotations()["test/done"] == "true"
    38  	expectedErr := data.GetAnnotations()["test/error"]
    39  
    40  	status, err := fn(data, rev)
    41  	if expectedErr != "" {
    42  		require.NotNil(t, err)
    43  		if strings.HasPrefix(expectedErr, "/") && strings.HasSuffix(expectedErr, "/") {
    44  			e := regexp.MustCompile(expectedErr[1 : len(expectedErr)-1])
    45  			a.Regexp(e, err.Error())
    46  			return
    47  		}
    48  		a.Equal(expectedErr, err.Error())
    49  		return
    50  	}
    51  
    52  	require.Nil(t, err)
    53  	require.NotNil(t, status)
    54  	a.Equal(expectedDesc, status.Description)
    55  	a.Equal(expectedDone, status.Done)
    56  }
    57  
    58  func testDir(t *testing.T, dir string) {
    59  	files, err := filepath.Glob(filepath.Join("testdata", dir, "*.json"))
    60  	require.Nil(t, err)
    61  	for _, file := range files {
    62  		t.Run(file, func(t *testing.T) {
    63  			un := load(t, file)
    64  			var rev int64
    65  			inputRevStr := un.GetAnnotations()["test-input/revision"]
    66  			if inputRevStr != "" {
    67  				var err error
    68  				rev, err = strconv.ParseInt(inputRevStr, 10, 64)
    69  				require.Nil(t, err)
    70  			}
    71  			statusFn := StatusFuncFor(model.NewK8sObject(un.Object))
    72  			require.NotNil(t, statusFn)
    73  			checkExpectedStatus(t, un, rev, statusFn)
    74  		})
    75  	}
    76  }
    77  
    78  func TestDeployStatus(t *testing.T) {
    79  	testDir(t, "deploy")
    80  }
    81  
    82  func TestDaemonSetStatus(t *testing.T) {
    83  	testDir(t, "daemonset")
    84  }
    85  
    86  func TestStatefulSetStatus(t *testing.T) {
    87  	testDir(t, "statefulset")
    88  }
    89  
    90  func TestUnknownObject(t *testing.T) {
    91  	obj := model.NewK8sObject(map[string]interface{}{
    92  		"kind":       "foo",
    93  		"apiversion": "apps/v1",
    94  		"metadata": map[string]interface{}{
    95  			"namespace": "foo",
    96  			"name":      "foo",
    97  		},
    98  	})
    99  	statusFn := StatusFuncFor(obj)
   100  	require.Nil(t, statusFn)
   101  }