github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/projecteru2/core/cluster"
    13  	"github.com/projecteru2/core/types"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRandomString(t *testing.T) {
    19  	s1 := RandomString(10)
    20  	assert.Equal(t, 10, len(s1))
    21  	s2 := RandomString(10)
    22  	assert.Equal(t, 10, len(s2))
    23  	assert.NotEqual(t, s1, s2, fmt.Sprintf("s1: %s, s2: %s", s1, s2))
    24  }
    25  
    26  func TestTail(t *testing.T) {
    27  	r1 := Tail("")
    28  	assert.Equal(t, r1, "")
    29  	r2 := Tail("/")
    30  	assert.Equal(t, r2, "")
    31  	r3 := Tail("a/b")
    32  	assert.Equal(t, r3, "b")
    33  	r4 := Tail("a/b/c")
    34  	assert.Equal(t, r4, "c")
    35  }
    36  
    37  func TestGetGitRepoName(t *testing.T) {
    38  	_, err := GetGitRepoName("xxx")
    39  	assert.Error(t, err)
    40  
    41  	_, err = GetGitRepoName("file://github.com/projecteru2/core.git")
    42  	assert.Error(t, err)
    43  
    44  	_, err = GetGitRepoName("https://github.com/projecteru2/core.git")
    45  	assert.NoError(t, err)
    46  
    47  	r1, err := GetGitRepoName("git@github.com:projecteru2/core.git")
    48  	assert.NoError(t, err)
    49  	assert.Equal(t, r1, "core")
    50  }
    51  
    52  func TestGetTag(t *testing.T) {
    53  	v := GetTag("xx")
    54  	assert.Equal(t, v, DefaultVersion)
    55  	v = GetTag("xx:1:2")
    56  	assert.Equal(t, v, "2")
    57  	v = GetTag("xx:2")
    58  	assert.Equal(t, v, "2")
    59  }
    60  
    61  func TestNormalizeImageName(t *testing.T) {
    62  	i := NormalizeImageName("image")
    63  	assert.Equal(t, i, "image:latest")
    64  	i = NormalizeImageName("image:1")
    65  	assert.Equal(t, i, "image:1")
    66  }
    67  
    68  func TestMakeCommandLine(t *testing.T) {
    69  	r1 := MakeCommandLineArgs("/bin/bash -l -c 'echo \"foo bar bah bin\"'")
    70  	assert.Equal(t, r1, []string{"/bin/bash", "-l", "-c", "echo \"foo bar bah bin\""})
    71  	r2 := MakeCommandLineArgs(" test -a   -b   -d")
    72  	assert.Equal(t, r2, []string{"test", "-a", "-b", "-d"})
    73  }
    74  
    75  func TestMakeWorkloadName(t *testing.T) {
    76  	r1 := MakeWorkloadName("test_appname", "web", "whatever")
    77  	assert.Equal(t, r1, "test_appname_web_whatever")
    78  	appname, entrypoint, ident, err := ParseWorkloadName("test_appname_web_whatever")
    79  	assert.Equal(t, appname, "test_appname")
    80  	assert.Equal(t, entrypoint, "web")
    81  	assert.Equal(t, ident, "whatever")
    82  	assert.Equal(t, err, nil)
    83  }
    84  
    85  func TestParseWorkloadName(t *testing.T) {
    86  	appname := "test_bad"
    87  	p1, p2, p3, err := ParseWorkloadName(appname)
    88  	assert.Error(t, err)
    89  	assert.Equal(t, p1, "")
    90  	assert.Equal(t, p2, "")
    91  	assert.Equal(t, p3, "")
    92  	appname = "test_good_name_1"
    93  	p1, p2, p3, err = ParseWorkloadName(appname)
    94  	assert.NoError(t, err)
    95  	assert.Equal(t, p1, "test_good")
    96  	assert.Equal(t, p2, "name")
    97  	assert.Equal(t, p3, "1")
    98  }
    99  
   100  func TestPublishInfo(t *testing.T) {
   101  	ports := []string{"123", "233"}
   102  	n1 := map[string]string{
   103  		"n1":   "233.233.233.233",
   104  		"host": "127.0.0.1",
   105  	}
   106  	r := MakePublishInfo(n1, ports)
   107  	assert.Equal(t, len(r), 2)
   108  	assert.Equal(t, len(r["n1"]), 2)
   109  	assert.Equal(t, r["n1"][0], "233.233.233.233:123")
   110  	assert.Equal(t, r["n1"][1], "233.233.233.233:233")
   111  	assert.Equal(t, len(r["host"]), 2)
   112  	assert.Equal(t, r["host"][0], "127.0.0.1:123")
   113  	assert.Equal(t, r["host"][1], "127.0.0.1:233")
   114  
   115  	e := EncodePublishInfo(r)
   116  	assert.Equal(t, len(e), 2)
   117  	assert.Equal(t, e["n1"], "233.233.233.233:123,233.233.233.233:233")
   118  	assert.Equal(t, e["host"], "127.0.0.1:123,127.0.0.1:233")
   119  
   120  	r2 := DecodePublishInfo(e)
   121  	assert.Equal(t, len(r2), 2)
   122  	assert.Equal(t, len(r2["n1"]), 2)
   123  	assert.Equal(t, len(r2["host"]), 2)
   124  	assert.Equal(t, r2["n1"][0], "233.233.233.233:123")
   125  	assert.Equal(t, r2["n1"][1], "233.233.233.233:233")
   126  	assert.Equal(t, r2["host"][0], "127.0.0.1:123")
   127  	assert.Equal(t, r2["host"][1], "127.0.0.1:233")
   128  }
   129  
   130  func TestMetaInLabel(t *testing.T) {
   131  	meta := &types.LabelMeta{
   132  		Publish: []string{"1", "2"},
   133  	}
   134  	r := EncodeMetaInLabel(context.Background(), meta)
   135  	assert.NotEmpty(t, r)
   136  
   137  	labels := map[string]string{
   138  		cluster.LabelMeta: "{\"Publish\":[\"5001\"],\"HealthCheck\":{\"TCPPorts\":[\"5001\"],\"HTTPPort\":\"\",\"HTTPURL\":\"\",\"HTTPCode\":0}}",
   139  	}
   140  	meta2 := DecodeMetaInLabel(context.Background(), labels)
   141  	assert.Equal(t, meta2.Publish[0], "5001")
   142  	meta3 := DecodeMetaInLabel(context.Background(), map[string]string{cluster.LabelMeta: ""})
   143  	assert.Nil(t, meta3.HealthCheck)
   144  }
   145  
   146  func TestShortID(t *testing.T) {
   147  	r1 := ShortID("1234")
   148  	assert.Equal(t, r1, "1234")
   149  	r2 := ShortID("12345678")
   150  	assert.Equal(t, r2, "2345678")
   151  }
   152  
   153  func TestLabelsFilter(t *testing.T) {
   154  	e := map[string]string{"a": "b"}
   155  	l := map[string]string{"a": "b"}
   156  	assert.True(t, LabelsFilter(e, l))
   157  	l["c"] = "d"
   158  	assert.False(t, LabelsFilter(e, l))
   159  }
   160  
   161  func TestCleanStatsdMetrics(t *testing.T) {
   162  	k := "a.b.c"
   163  	assert.Equal(t, CleanStatsdMetrics(k), "a-b-c")
   164  }
   165  
   166  func TestTempFile(t *testing.T) {
   167  	buff := bytes.NewBufferString("test")
   168  	rc := io.NopCloser(buff)
   169  	fname, err := TempFile(rc)
   170  	assert.NoError(t, err)
   171  	f, err := os.Open(fname)
   172  	assert.NoError(t, err)
   173  	b, err := io.ReadAll(f)
   174  	assert.NoError(t, err)
   175  	assert.Equal(t, string(b), "test")
   176  	os.Remove(fname)
   177  }
   178  
   179  func TestRound(t *testing.T) {
   180  	a := 0.0199999998
   181  	assert.InDelta(t, Round(a), 0.02, 1e-5)
   182  	a = 0.1999998
   183  	assert.InDelta(t, Round(a), 0.2, 1e-5)
   184  	a = 1.999998
   185  	assert.InDelta(t, Round(a), 1.999998, 1e-6)
   186  	a = 19.99998
   187  	assert.InDelta(t, (Round(a)), 19.99998, 1e-6)
   188  }
   189  
   190  func TestMergeHookOutputs(t *testing.T) {
   191  	test := []*bytes.Buffer{bytes.NewBufferString("a"), bytes.NewBufferString("b")}
   192  	r := MergeHookOutputs(test)
   193  	assert.NotEmpty(t, r)
   194  	assert.Equal(t, string(r), "ab")
   195  }
   196  
   197  func TestEnsureReaderClosed(t *testing.T) {
   198  	EnsureReaderClosed(context.Background(), nil)
   199  	s := io.NopCloser(bytes.NewBuffer([]byte{10, 10, 10}))
   200  	EnsureReaderClosed(context.Background(), s)
   201  }
   202  
   203  func TestRange(t *testing.T) {
   204  	res := Range(10)
   205  	assert.Equal(t, 10, len(res))
   206  	for i := 0; i < 10; i++ {
   207  		assert.Equal(t, i, res[i])
   208  	}
   209  }
   210  
   211  func TestWithTimeout(t *testing.T) {
   212  	r := true
   213  	f := func(context.Context) {
   214  		r = false
   215  	}
   216  	WithTimeout(context.Background(), time.Second, f)
   217  	assert.False(t, r)
   218  }
   219  
   220  func TestSHA256(t *testing.T) {
   221  	str := "hhh"
   222  	assert.Equal(t, "24d166cd6c8b826c779040b49d5b6708d649b236558e8744339dfee6afe11999", SHA256(str))
   223  }
   224  
   225  func TestBool2Int(t *testing.T) {
   226  	assert.Equal(t, 1, Bool2Int(true))
   227  	assert.Equal(t, 0, Bool2Int(false))
   228  }