github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/build_test.go (about) 1 package calcium 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "io" 8 "testing" 9 10 enginemocks "github.com/projecteru2/core/engine/mocks" 11 resourcemocks "github.com/projecteru2/core/resource/mocks" 12 storemocks "github.com/projecteru2/core/store/mocks" 13 "github.com/projecteru2/core/types" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/mock" 16 ) 17 18 const ( 19 base = "alpine:latest" 20 repo = "https://test/repo.git" 21 ) 22 23 // test no tags 24 func TestBuild(t *testing.T) { 25 c := NewTestCluster() 26 ctx := context.Background() 27 opts := &types.BuildOptions{ 28 Name: "xx", 29 BuildMethod: types.BuildFromSCM, 30 Builds: &types.Builds{ 31 Stages: []string{"compile", "build"}, 32 Builds: map[string]*types.Build{ 33 "compile": { 34 Base: base, 35 Repo: repo, 36 Version: "version", 37 Artifacts: map[string]string{"url1": "/path1", "url2": "/path2"}, 38 Cache: map[string]string{"/src1": "/dst1", "/src2": "/dst2"}, 39 Commands: []string{"cmd1", "cmd2"}, 40 }, 41 "build": { 42 Base: base, 43 Commands: []string{"cmd1", "cmd2"}, 44 Args: map[string]string{"args1": "a", "args2": "b"}, 45 Envs: map[string]string{"envs1": "a", "envs2": "b"}, 46 Labels: map[string]string{"labels": "a", "label2": "b"}, 47 Dir: "/tmp", 48 }, 49 }, 50 }, 51 UID: 1234, 52 User: "test", 53 Tags: []string{"tag1", "tag2"}, 54 } 55 // failed by no source 56 c.source = nil 57 _, err := c.BuildImage(ctx, opts) 58 assert.Error(t, err) 59 // failed by buildpod not set 60 c = NewTestCluster() 61 _, err = c.BuildImage(ctx, opts) 62 assert.Error(t, err) 63 c.config.Docker.BuildPod = "test" 64 // failed by ListPodNodes failed 65 store := c.store.(*storemocks.Store) 66 store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrInvaildWorkloadMeta).Once() 67 ch, err := c.BuildImage(ctx, opts) 68 assert.Error(t, err) 69 // failed by no nodes 70 store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{}, nil).Once() 71 ch, err = c.BuildImage(ctx, opts) 72 assert.Error(t, err) 73 engine := &enginemocks.API{} 74 node := &types.Node{ 75 NodeMeta: types.NodeMeta{ 76 Name: "test", 77 Podname: "testpod", 78 }, 79 Available: true, 80 Engine: engine, 81 } 82 store.On("GetNodesByPod", mock.Anything, mock.Anything).Return([]*types.Node{node}, nil) 83 // failed by plugin error 84 rmgr := c.rmgr.(*resourcemocks.Manager) 85 rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil) 86 rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("", types.ErrInvaildCount).Once() 87 ch, err = c.BuildImage(ctx, opts) 88 assert.Error(t, err) 89 rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("test", nil) 90 // create image 91 c.config.Docker.Hub = "test.com" 92 c.config.Docker.Namespace = "test" 93 94 buildImageMessage := &types.BuildImageMessage{} 95 buildImageMessage.Progress = "process" 96 buildImageMessage.Error = "" 97 buildImageMessage.ID = "ID1234" 98 buildImageMessage.Status = "status" 99 buildImageMessage.Stream = "stream" 100 buildImageMessage.ErrorDetail.Code = 0 101 buildImageResp, err := json.Marshal(buildImageMessage) 102 assert.NoError(t, err) 103 buildImageResp2, err := json.Marshal(buildImageMessage) 104 assert.NoError(t, err) 105 buildImageRespReader := io.NopCloser(bytes.NewReader(buildImageResp)) 106 buildImageRespReader2 := io.NopCloser(bytes.NewReader(buildImageResp2)) 107 engine.On("BuildRefs", mock.Anything, mock.Anything, mock.Anything).Return([]string{"t1", "t2"}) 108 // failed by build context 109 engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", nil, types.ErrInvaildCount).Once() 110 ch, err = c.BuildImage(ctx, opts) 111 assert.Error(t, err) 112 b := io.NopCloser(bytes.NewReader([]byte{})) 113 engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", b, nil) 114 // failed by ImageBuild 115 opts.BuildMethod = types.BuildFromRaw 116 engine.On("ImageBuild", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNilEngine).Once() 117 ch, err = c.BuildImage(ctx, opts) 118 assert.Error(t, err) 119 // build from exist not implemented 120 opts.BuildMethod = types.BuildFromExist 121 opts.ExistID = "123" 122 engine.On("ImageBuildFromExist", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return("", types.ErrEngineNotImplemented).Once() 123 store.On("GetWorkload", mock.Anything, mock.Anything).Return(&types.Workload{Nodename: "123"}, nil) 124 store.On("GetNode", mock.Anything, mock.Anything).Return(&types.Node{Engine: engine}, nil) 125 ch, err = c.BuildImage(ctx, opts) 126 assert.EqualError(t, err, types.ErrEngineNotImplemented.Error()) 127 // unknown build method 128 opts.BuildMethod = types.BuildFromUnknown 129 ch, err = c.BuildImage(ctx, opts) 130 assert.Error(t, err) 131 // correct 132 engine.On("ImageBuild", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(buildImageRespReader, nil) 133 engine.On("ImagePush", mock.Anything, mock.Anything).Return(buildImageRespReader2, nil) 134 engine.On("ImageRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil) 135 engine.On("ImageBuildCachePrune", mock.Anything, mock.Anything).Return(uint64(1024), nil) 136 engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", nil, nil) 137 opts.BuildMethod = types.BuildFromSCM 138 ch, err = c.BuildImage(ctx, opts) 139 if assert.NoError(t, err) { 140 for range ch { 141 assert.NoError(t, err) 142 } 143 } 144 }