File: flags_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (71 lines) | stat: -rw-r--r-- 1,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build !integration
// +build !integration

package featureflags

import (
	"testing"

	logrustest "github.com/sirupsen/logrus/hooks/test"
	"github.com/stretchr/testify/assert"
)

func mockFlags(newFlags ...FeatureFlag) func() {
	oldFlags := flags
	flags = newFlags

	return func() {
		flags = oldFlags
	}
}

func TestGetAll(t *testing.T) {
	testFlag := FeatureFlag{Name: "TEST_FLAG", DefaultValue: true}

	defer mockFlags(testFlag)()

	f := GetAll()
	assert.Len(t, f, 1)
	assert.Contains(t, f, testFlag)
}

func TestIsOn(t *testing.T) {
	testCases := map[string]struct {
		testValue      string
		expectedResult bool
		expectedLog    bool
	}{
		"empty value": {
			testValue:      "",
			expectedResult: false,
		},
		"non boolean value": {
			testValue:      "a",
			expectedResult: false,
			expectedLog:    true,
		},
		"true value": {
			testValue:      "1",
			expectedResult: true,
		},
		"false value": {
			testValue:      "f",
			expectedResult: false,
		},
	}

	for testName, testCase := range testCases {
		t.Run(testName, func(t *testing.T) {
			logger, hook := logrustest.NewNullLogger()
			result := IsOn(logger, testCase.testValue)
			assert.Equal(t, testCase.expectedResult, result)
			if testCase.expectedLog {
				assert.NotNil(t, hook.LastEntry())
				assert.Contains(t, "Error while parsing the value of feature flag", hook.LastEntry().Message)
				return
			}

			assert.Nil(t, hook.LastEntry())
		})
	}
}