How to use NewController method of gomock Package

Best Mock code snippet using gomock.NewController

ut_router_test.go

Source:ut_router_test.go Github

copy

Full Screen

...23	"github.com/ZTE/Knitter/pkg/iaas-accessor"24	"github.com/golang/gostub"25)26func TestCreateRouterErrNotCfgIaaS(t *testing.T) {27	cfgMock := gomock.NewController(t)28	defer cfgMock.Finish()29	mockDB := NewMockDbAccessor(cfgMock)30	common.SetDataBase(mockDB)31	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)32	defer stubs.Reset()33	MockPaasAdminCheck(mockDB)34	cfg := string(`{"router":{"name":"test-create-router"}}`)35	resp := CreateRouter(cfg)36	Convey("TestCreateRouterErrNotCfgIaaS\n", t, func() {37		So(resp.Code, ShouldEqual, 401)38	})39}40func TestCreateRouterErr403(t *testing.T) {41	cfgMock := gomock.NewController(t)42	defer cfgMock.Finish()43	mockDB := NewMockDbAccessor(cfgMock)44	mockIaaS := NewMockIaaS(cfgMock)45	common.SetDataBase(mockDB)46	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)47	defer stubs.Reset()48	MockPaasAdminCheck(mockDB)49	cfg := string(`{"router":{"name":"test-create-router"}`)50	resp := CreateRouter(cfg)51	Convey("TestCreateNetworkErrNotCfgIaaS\n", t, func() {52		So(resp.Code, ShouldEqual, 403)53	})54}55func TestCreateRouterErr406A(t *testing.T) {56	cfgMock := gomock.NewController(t)57	defer cfgMock.Finish()58	mockDB := NewMockDbAccessor(cfgMock)59	mockIaaS := NewMockIaaS(cfgMock)60	common.SetDataBase(mockDB)61	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)62	defer stubs.Reset()63	MockPaasAdminCheck(mockDB)64	mockIaaS.EXPECT().CreateRouter(gomock.Any(),65		gomock.Any()).Return("", errors.New("create-router-error"))66	cfg := string(`{"router":{"name":"test-create-router"}}`)67	resp := CreateRouter(cfg)68	Convey("TestCreateRouterErr406A\n", t, func() {69		So(resp.Code, ShouldEqual, 406)70	})71}72func TestCreateRouterErr406B(t *testing.T) {73	cfgMock := gomock.NewController(t)74	defer cfgMock.Finish()75	mockDB := NewMockDbAccessor(cfgMock)76	mockIaaS := NewMockIaaS(cfgMock)77	common.SetDataBase(mockDB)78	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)79	defer stubs.Reset()80	MockPaasAdminCheck(mockDB)81	mockIaaS.EXPECT().CreateRouter(gomock.Any(),82		gomock.Any()).Return("router-uuid", nil)83	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(84		nil, errors.New("get-router-error"))85	cfg := string(`{"router":{"name":"test-create-router"}}`)86	resp := CreateRouter(cfg)87	Convey("TestCreateRouterErr406B\n", t, func() {88		So(resp.Code, ShouldEqual, 406)89	})90}91func TestCreateRouterErr406C(t *testing.T) {92	cfgMock := gomock.NewController(t)93	defer cfgMock.Finish()94	mockDB := NewMockDbAccessor(cfgMock)95	mockIaaS := NewMockIaaS(cfgMock)96	common.SetDataBase(mockDB)97	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)98	defer stubs.Reset()99	MockPaasAdminCheck(mockDB)100	mockIaaS.EXPECT().CreateRouter(gomock.Any(),101		gomock.Any()).Return("router-uuid", nil)102	router := iaasaccessor.Router{Id: "router-uuid"}103	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(104		&router, nil)105	mockDB.EXPECT().SaveLeaf(gomock.Any(),106		gomock.Any()).Return(errors.New("error"))107	mockIaaS.EXPECT().DeleteRouter(gomock.Any()).Return(nil)108	cfg := string(`{"router":{"name":"test-create-router"}}`)109	resp := CreateRouter(cfg)110	Convey("TestCreateRouterErr406C\n", t, func() {111		So(resp.Code, ShouldEqual, 406)112	})113}114func TestCreateRouterOK(t *testing.T) {115	cfgMock := gomock.NewController(t)116	defer cfgMock.Finish()117	mockDB := NewMockDbAccessor(cfgMock)118	mockIaaS := NewMockIaaS(cfgMock)119	common.SetDataBase(mockDB)120	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)121	defer stubs.Reset()122	MockPaasAdminCheck(mockDB)123	mockIaaS.EXPECT().CreateRouter(gomock.Any(),124		gomock.Any()).Return("router-uuid", nil)125	router := iaasaccessor.Router{Id: "router-uuid"}126	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(127		&router, nil)128	mockDB.EXPECT().SaveLeaf(gomock.Any(),129		gomock.Any()).Return(nil)130	cfg := string(`{"router":{"name":"test-create-router"}}`)131	resp := CreateRouter(cfg)132	Convey("TestCreateRouterOK\n", t, func() {133		So(resp.Code, ShouldEqual, 200)134	})135}136func TestDeleteRouterERR401(t *testing.T) {137	cfgMock := gomock.NewController(t)138	defer cfgMock.Finish()139	mockDB := NewMockDbAccessor(cfgMock)140	common.SetDataBase(mockDB)141	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)142	defer stubs.Reset()143	MockPaasAdminCheck(mockDB)144	resp := DeleteRouter("the-uuid-of-router")145	Convey("TestDeleteRouterERR401\n", t, func() {146		So(resp.Code, ShouldEqual, 401)147	})148}149func TestDeleteRouterERR404(t *testing.T) {150	cfgMock := gomock.NewController(t)151	defer cfgMock.Finish()152	mockDB := NewMockDbAccessor(cfgMock)153	mockIaaS := NewMockIaaS(cfgMock)154	common.SetDataBase(mockDB)155	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)156	defer stubs.Reset()157	MockPaasAdminCheck(mockDB)158	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(159		"", errors.New("read-router-return-error"))160	resp := DeleteRouter("the-uuid-of-router")161	Convey("TestDeleteRouterERR404\n", t, func() {162		So(resp.Code, ShouldEqual, 404)163	})164}165func TestDeleteRouter404A(t *testing.T) {166	cfgMock := gomock.NewController(t)167	defer cfgMock.Finish()168	mockDB := NewMockDbAccessor(cfgMock)169	mockIaaS := NewMockIaaS(cfgMock)170	common.SetDataBase(mockDB)171	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)172	defer stubs.Reset()173	MockPaasAdminCheck(mockDB)174	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(175		"router-uuid", nil)176	mockIaaS.EXPECT().DeleteRouter(gomock.Any()).Return(177		errors.New("error"))178	resp := DeleteRouter("the-uuid-of-router")179	Convey("TestDeleteRouter404A\n", t, func() {180		So(resp.Code, ShouldEqual, 404)181	})182}183func TestDeleteRouter404B(t *testing.T) {184	cfgMock := gomock.NewController(t)185	defer cfgMock.Finish()186	mockDB := NewMockDbAccessor(cfgMock)187	mockIaaS := NewMockIaaS(cfgMock)188	common.SetDataBase(mockDB)189	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)190	defer stubs.Reset()191	MockPaasAdminCheck(mockDB)192	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(193		"router-uuid", nil)194	mockIaaS.EXPECT().DeleteRouter(gomock.Any()).Return(nil)195	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(196		errors.New("error"))197	resp := DeleteRouter("the-uuid-of-router")198	Convey("TestDeleteRouter404B\n", t, func() {199		So(resp.Code, ShouldEqual, 404)200	})201}202func TestDeleteRouter404C(t *testing.T) {203	cfgMock := gomock.NewController(t)204	defer cfgMock.Finish()205	mockDB := NewMockDbAccessor(cfgMock)206	mockIaaS := NewMockIaaS(cfgMock)207	common.SetDataBase(mockDB)208	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)209	defer stubs.Reset()210	MockPaasAdminCheck(mockDB)211	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(212		"router-uuid", nil)213	mockIaaS.EXPECT().DeleteRouter(gomock.Any()).Return(nil)214	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(nil)215	mockDB.EXPECT().DeleteDir(gomock.Any()).Return(216		errors.New("error"))217	resp := DeleteRouter("the-uuid-of-router")218	Convey("TestDeleteRouter404C\n", t, func() {219		So(resp.Code, ShouldEqual, 404)220	})221}222func TestDeleteRouterOK(t *testing.T) {223	cfgMock := gomock.NewController(t)224	defer cfgMock.Finish()225	mockDB := NewMockDbAccessor(cfgMock)226	mockIaaS := NewMockIaaS(cfgMock)227	common.SetDataBase(mockDB)228	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)229	defer stubs.Reset()230	MockPaasAdminCheck(mockDB)231	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(232		"router-uuid", nil)233	mockIaaS.EXPECT().DeleteRouter(gomock.Any()).Return(nil)234	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(nil)235	mockDB.EXPECT().DeleteDir(gomock.Any()).Return(nil)236	resp := DeleteRouter("the-uuid-of-router")237	Convey("TestDeleteRouterOK\n", t, func() {238		So(resp.Code, ShouldEqual, 200)239	})240}241func TestGetRouterERR401(t *testing.T) {242	cfgMock := gomock.NewController(t)243	defer cfgMock.Finish()244	mockDB := NewMockDbAccessor(cfgMock)245	common.SetDataBase(mockDB)246	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)247	defer stubs.Reset()248	MockPaasAdminCheck(mockDB)249	resp := GetRouter("the-uuid-of-router")250	Convey("TestGetRouterERR401\n", t, func() {251		So(resp.Code, ShouldEqual, 401)252	})253}254func TestGetRouterERR404B(t *testing.T) {255	cfgMock := gomock.NewController(t)256	defer cfgMock.Finish()257	mockDB := NewMockDbAccessor(cfgMock)258	mockIaaS := NewMockIaaS(cfgMock)259	common.SetDataBase(mockDB)260	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)261	defer stubs.Reset()262	MockPaasAdminCheck(mockDB)263	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(264		"", errors.New("read-router-from-etcd-error"))265	resp := GetRouter("the-uuid-of-router")266	Convey("TestGetRouterERR404B\n", t, func() {267		So(resp.Code, ShouldEqual, 404)268	})269}270func TestGetRouterOK(t *testing.T) {271	cfgMock := gomock.NewController(t)272	defer cfgMock.Finish()273	mockDB := NewMockDbAccessor(cfgMock)274	mockIaaS := NewMockIaaS(cfgMock)275	common.SetDataBase(mockDB)276	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)277	defer stubs.Reset()278	MockPaasAdminCheck(mockDB)279	router := string(`{280    "router": {281        "name": "router_show",282        "id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"283    }}`)284	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(285		router, nil)286	resp := GetRouter("the-uuid-of-router")287	Convey("TestGetRouterOK\n", t, func() {288		So(resp.Code, ShouldEqual, 200)289	})290}291var Config string = `{292		"router":{293			"name":"auto-test-create-router-should-be-delete"294			}295		}`296var errCfg string = `{297		"router":{298			"name":"auto-test-create-router-should-be-delete"299		}`300func TestUpdateRouterERR401(t *testing.T) {301	cfgMock := gomock.NewController(t)302	defer cfgMock.Finish()303	mockDB := NewMockDbAccessor(cfgMock)304	common.SetDataBase(mockDB)305	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)306	defer stubs.Reset()307	MockPaasAdminCheck(mockDB)308	resp := UpdateRouter("the-uuid-of-router", Config)309	Convey("TestGetRouterERR401\n", t, func() {310		So(resp.Code, ShouldEqual, 401)311	})312}313func TestUpdateRouterERR403(t *testing.T) {314	cfgMock := gomock.NewController(t)315	defer cfgMock.Finish()316	mockDB := NewMockDbAccessor(cfgMock)317	mockIaaS := NewMockIaaS(cfgMock)318	common.SetDataBase(mockDB)319	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)320	defer stubs.Reset()321	MockPaasAdminCheck(mockDB)322	resp := UpdateRouter("the-uuid-of-router", errCfg)323	Convey("TestUpdateRouterERR403\n", t, func() {324		So(resp.Code, ShouldEqual, 403)325	})326}327func TestUpdateRouterERR406A(t *testing.T) {328	cfgMock := gomock.NewController(t)329	defer cfgMock.Finish()330	mockDB := NewMockDbAccessor(cfgMock)331	mockIaaS := NewMockIaaS(cfgMock)332	common.SetDataBase(mockDB)333	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)334	defer stubs.Reset()335	MockPaasAdminCheck(mockDB)336	mockIaaS.EXPECT().UpdateRouter(gomock.Any(),337		gomock.Any(), gomock.Any()).Return(errors.New("error"))338	resp := UpdateRouter("the-uuid-of-router", Config)339	Convey("TestUpdateRouterERR406A\n", t, func() {340		So(resp.Code, ShouldEqual, 406)341	})342}343func TestUpdateRouterERR406B(t *testing.T) {344	cfgMock := gomock.NewController(t)345	defer cfgMock.Finish()346	mockDB := NewMockDbAccessor(cfgMock)347	mockIaaS := NewMockIaaS(cfgMock)348	common.SetDataBase(mockDB)349	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)350	defer stubs.Reset()351	MockPaasAdminCheck(mockDB)352	mockIaaS.EXPECT().UpdateRouter(gomock.Any(),353		gomock.Any(), gomock.Any()).Return(nil)354	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(355		nil, errors.New("error"))356	resp := UpdateRouter("the-uuid-of-router", Config)357	Convey("TestUpdateRouterERR406B\n", t, func() {358		So(resp.Code, ShouldEqual, 406)359	})360}361func TestUpdateRouterERR406C(t *testing.T) {362	cfgMock := gomock.NewController(t)363	defer cfgMock.Finish()364	mockDB := NewMockDbAccessor(cfgMock)365	mockIaaS := NewMockIaaS(cfgMock)366	common.SetDataBase(mockDB)367	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)368	defer stubs.Reset()369	MockPaasAdminCheck(mockDB)370	createRouter := iaasaccessor.Router{Id: "router-uuid", Name: "name-router"}371	mockIaaS.EXPECT().UpdateRouter(gomock.Any(),372		gomock.Any(), gomock.Any()).Return(nil)373	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(374		&createRouter, nil)375	mockDB.EXPECT().SaveLeaf(gomock.Any(),376		gomock.Any()).Return(errors.New("error"))377	resp := UpdateRouter("the-uuid-of-router", Config)378	Convey("TestUpdateRouterERR406B\n", t, func() {379		So(resp.Code, ShouldEqual, 406)380	})381}382func TestUpdateRouterOK(t *testing.T) {383	cfgMock := gomock.NewController(t)384	defer cfgMock.Finish()385	mockDB := NewMockDbAccessor(cfgMock)386	mockIaaS := NewMockIaaS(cfgMock)387	common.SetDataBase(mockDB)388	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)389	defer stubs.Reset()390	MockPaasAdminCheck(mockDB)391	createRouter := iaasaccessor.Router{Id: "router-uuid", Name: "name-router"}392	mockIaaS.EXPECT().UpdateRouter(gomock.Any(),393		gomock.Any(), gomock.Any()).Return(nil)394	mockIaaS.EXPECT().GetRouter(gomock.Any()).Return(395		&createRouter, nil)396	mockDB.EXPECT().SaveLeaf(gomock.Any(),397		gomock.Any()).Return(nil)398	resp := UpdateRouter("the-uuid-of-router", Config)399	Convey("TestUpdateRouterOK\n", t, func() {400		So(resp.Code, ShouldEqual, 200)401	})402}403func TestGetAllRouterERR401(t *testing.T) {404	cfgMock := gomock.NewController(t)405	defer cfgMock.Finish()406	mockDB := NewMockDbAccessor(cfgMock)407	common.SetDataBase(mockDB)408	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)409	defer stubs.Reset()410	MockPaasAdminCheck(mockDB)411	resp := GetAllRouter()412	Convey("TestGetAllRouterERR401\n", t, func() {413		So(resp.Code, ShouldEqual, 401)414	})415}416func TestGetAllRouterOK1(t *testing.T) {417	cfgMock := gomock.NewController(t)418	defer cfgMock.Finish()419	mockDB := NewMockDbAccessor(cfgMock)420	mockIaaS := NewMockIaaS(cfgMock)421	common.SetDataBase(mockDB)422	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)423	defer stubs.Reset()424	MockPaasAdminCheck(mockDB)425	mockDB.EXPECT().ReadDir(gomock.Any()).Return(nil,426		errors.New("read-dir-error"))427	resp := GetAllRouter()428	Convey("TestGetAllRouterOK1\n", t, func() {429		So(resp.Code, ShouldEqual, 200)430	})431}432func TestGetAllRouterOK2(t *testing.T) {433	cfgMock := gomock.NewController(t)434	defer cfgMock.Finish()435	mockDB := NewMockDbAccessor(cfgMock)436	mockIaaS := NewMockIaaS(cfgMock)437	common.SetDataBase(mockDB)438	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)439	defer stubs.Reset()440	MockPaasAdminCheck(mockDB)441	mockDB.EXPECT().ReadDir(gomock.Any()).Return(nil, nil)442	resp := GetAllRouter()443	Convey("TestGetAllRouterOK2\n", t, func() {444		So(resp.Code, ShouldEqual, 200)445	})446}447func TestGetAllRouterOK3(t *testing.T) {448	cfgMock := gomock.NewController(t)449	defer cfgMock.Finish()450	mockDB := NewMockDbAccessor(cfgMock)451	mockIaaS := NewMockIaaS(cfgMock)452	common.SetDataBase(mockDB)453	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)454	defer stubs.Reset()455	MockPaasAdminCheck(mockDB)456	router := string(`{457    "router": {458        "name": "network_show",459        "id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"460    }}`)461	var list1 []*client.Node462	node := client.Node{Key: "network-uuid", Value: "network-info"}463	list1 = append(list1, &node)464	list1 = append(list1, &node)465	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list1, nil)466	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(router, nil)467	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return("",468		errors.New("read-leaf-error"))469	resp := GetAllRouter()470	Convey("TestGetAllRouterOK3\n", t, func() {471		So(resp.Code, ShouldEqual, 200)472	})473}474func TestAttachERR401(t *testing.T) {475	cfgMock := gomock.NewController(t)476	defer cfgMock.Finish()477	mockDB := NewMockDbAccessor(cfgMock)478	common.SetDataBase(mockDB)479	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)480	defer stubs.Reset()481	MockPaasAdminCheck(mockDB)482	resp := AttachRouter("uuid-for-routet", "uuid-for-network")483	Convey("TestAttachERR401\n", t, func() {484		So(resp.Code, ShouldEqual, 401)485	})486}487func TestAttachERR403(t *testing.T) {488	cfgMock := gomock.NewController(t)489	defer cfgMock.Finish()490	mockDB := NewMockDbAccessor(cfgMock)491	mockIaaS := NewMockIaaS(cfgMock)492	common.SetDataBase(mockDB)493	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)494	defer stubs.Reset()495	MockPaasAdminCheck(mockDB)496	resp := AttachRouter403("uuid-for-routet", "uuid-for-network")497	Convey("TestAttachERR403\n", t, func() {498		So(resp.Code, ShouldEqual, 403)499	})500}501func TestAttachERR406A(t *testing.T) {502	cfgMock := gomock.NewController(t)503	defer cfgMock.Finish()504	mockDB := NewMockDbAccessor(cfgMock)505	mockIaaS := NewMockIaaS(cfgMock)506	common.SetDataBase(mockDB)507	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)508	defer stubs.Reset()509	MockPaasAdminCheck(mockDB)510	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(511		"", errors.New("error"))512	resp := AttachRouter("uuid-for-routet", "uuid-for-network")513	Convey("TestAttachERR401\n", t, func() {514		So(resp.Code, ShouldEqual, 406)515	})516}517func TestAttachERR406B(t *testing.T) {518	cfgMock := gomock.NewController(t)519	defer cfgMock.Finish()520	mockDB := NewMockDbAccessor(cfgMock)521	mockIaaS := NewMockIaaS(cfgMock)522	common.SetDataBase(mockDB)523	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)524	defer stubs.Reset()525	MockPaasAdminCheck(mockDB)526	netInfo := string(`{527    "network": {528        "cidr": "123.124.125.0/24",529        "gateway": "123.124.125.1",530        "name": "network_show",531        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"532    }}`)533	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(534		netInfo, nil)535	mockIaaS.EXPECT().AttachNetToRouter(536		gomock.Any(), gomock.Any()).Return("", errors.New("error"))537	resp := AttachRouter("uuid-for-routet", "uuid-for-network")538	Convey("TestAttachERR401\n", t, func() {539		So(resp.Code, ShouldEqual, 406)540	})541}542func TestAttachERR406C(t *testing.T) {543	cfgMock := gomock.NewController(t)544	defer cfgMock.Finish()545	mockDB := NewMockDbAccessor(cfgMock)546	mockIaaS := NewMockIaaS(cfgMock)547	common.SetDataBase(mockDB)548	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)549	defer stubs.Reset()550	MockPaasAdminCheck(mockDB)551	netInfo := string(`{552    "network": {553        "cidr": "123.124.125.0/24",554        "gateway": "123.124.125.1",555        "name": "network_show",556        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"557    }}`)558	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(559		netInfo, nil)560	mockIaaS.EXPECT().AttachNetToRouter(gomock.Any(),561		gomock.Any()).Return("uuid-for-port", nil)562	mockDB.EXPECT().SaveLeaf(gomock.Any(),563		gomock.Any()).Return(nil)564	mockDB.EXPECT().SaveLeaf(gomock.Any(),565		gomock.Any()).Return(errors.New("error"))566	resp := AttachRouter("uuid-for-routet", "uuid-for-network")567	Convey("TestAttachERR401\n", t, func() {568		So(resp.Code, ShouldEqual, 406)569	})570}571/*572func TestAttachERR406D(t *testing.T) {573	cfgMock := gomock.NewController(t)574	defer cfgMock.Finish()575	mockDB := NewMockDbAccessor(cfgMock)576	mockIaaS := NewMockIaaS(cfgMock)577	common.SetDataBase(mockDB)578	common.SetIaaS(mockIaaS)579	netInfo := string(`{580    "network": {581        "cidr": "123.124.125.0/24",582        "gateway": "123.124.125.1",583        "name": "network_show",584        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"585    }}`)586	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(587		netInfo, nil)588	mockIaaS.EXPECT().AttachNetToRouter(gomock.Any(),589		gomock.Any()).Return("uuid-for-port",nil)590	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)591	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)592	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(593		errors.New("error"))594	resp := AttachRouter("uuid-for-routet","uuid-for-network")595	Convey("TestAttachERR406C\n", t, func() {596		So(resp.Code, ShouldEqual, 406)597	})598}599func TestAttachERR406E(t *testing.T) {600	cfgMock := gomock.NewController(t)601	defer cfgMock.Finish()602	mockDB := NewMockDbAccessor(cfgMock)603	mockIaaS := NewMockIaaS(cfgMock)604	common.SetDataBase(mockDB)605	common.SetIaaS(mockIaaS)606	netInfo := string(`{607    "network": {608        "cidr": "123.124.125.0/24",609        "gateway": "123.124.125.1",610        "name": "network_show",611        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"612    }}`)613	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(614		netInfo, nil)615	mockIaaS.EXPECT().AttachNetToRouter(gomock.Any(),616		gomock.Any()).Return("uuid-for-port",nil)617	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)618	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)619	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)620	mockDB.EXPECT().SaveLeaf(gomock.Any(),gomock.Any()).Return(nil)621	mockDB.EXPECT().SaveLeaf(gomock.Any(),622		gomock.Any()).Return(errors.New("error"))623	resp := AttachRouter("uuid-for-routet","uuid-for-network")624	Convey("TestAttachERR406C\n", t, func() {625		So(resp.Code, ShouldEqual, 406)626	})627}628*/629func TestAttachOK(t *testing.T) {630	cfgMock := gomock.NewController(t)631	defer cfgMock.Finish()632	mockDB := NewMockDbAccessor(cfgMock)633	mockIaaS := NewMockIaaS(cfgMock)634	common.SetDataBase(mockDB)635	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)636	defer stubs.Reset()637	MockPaasAdminCheck(mockDB)638	netInfo := string(`{639    "network": {640        "cidr": "123.124.125.0/24",641        "gateway": "123.124.125.1",642        "name": "network_show",643        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"644    }}`)645	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(646		netInfo, nil)647	mockIaaS.EXPECT().AttachNetToRouter(gomock.Any(),648		gomock.Any()).Return("uuid-for-port", nil)649	mockDB.EXPECT().SaveLeaf(gomock.Any(),650		gomock.Any()).Return(nil)651	mockDB.EXPECT().SaveLeaf(gomock.Any(),652		gomock.Any()).Return(nil)653	mockDB.EXPECT().SaveLeaf(gomock.Any(),654		gomock.Any()).Return(nil)655	mockDB.EXPECT().SaveLeaf(gomock.Any(),656		gomock.Any()).Return(nil)657	mockDB.EXPECT().SaveLeaf(gomock.Any(),658		gomock.Any()).Return(nil)659	mockDB.EXPECT().SaveLeaf(gomock.Any(),660		gomock.Any()).Return(nil)661	resp := AttachRouter("uuid-for-routet", "uuid-for-network")662	Convey("TestAttachERR406D\n", t, func() {663		So(resp.Code, ShouldEqual, 200)664	})665}666func TestDetachERR401(t *testing.T) {667	cfgMock := gomock.NewController(t)668	defer cfgMock.Finish()669	mockDB := NewMockDbAccessor(cfgMock)670	common.SetDataBase(mockDB)671	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)672	defer stubs.Reset()673	MockPaasAdminCheck(mockDB)674	resp := DetachRouter("uuid-for-routet", "uuid-for-network")675	Convey("TestDetachERR401\n", t, func() {676		So(resp.Code, ShouldEqual, 401)677	})678}679func TestDetachERR403(t *testing.T) {680	cfgMock := gomock.NewController(t)681	defer cfgMock.Finish()682	mockDB := NewMockDbAccessor(cfgMock)683	mockIaaS := NewMockIaaS(cfgMock)684	common.SetDataBase(mockDB)685	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)686	defer stubs.Reset()687	MockPaasAdminCheck(mockDB)688	resp := DetachRouter403("uuid-for-routet", "uuid-for-network")689	Convey("TestDetachERR403\n", t, func() {690		So(resp.Code, ShouldEqual, 403)691	})692}693func TestDetachERR406A(t *testing.T) {694	cfgMock := gomock.NewController(t)695	defer cfgMock.Finish()696	mockDB := NewMockDbAccessor(cfgMock)697	mockIaaS := NewMockIaaS(cfgMock)698	common.SetDataBase(mockDB)699	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)700	defer stubs.Reset()701	MockPaasAdminCheck(mockDB)702	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(703		"", errors.New("error"))704	resp := DetachRouter("uuid-for-routet", "uuid-for-network")705	Convey("TestAttachERR401\n", t, func() {706		So(resp.Code, ShouldEqual, 406)707	})708}709func TestDetachERR406B(t *testing.T) {710	cfgMock := gomock.NewController(t)711	defer cfgMock.Finish()712	mockDB := NewMockDbAccessor(cfgMock)713	mockIaaS := NewMockIaaS(cfgMock)714	common.SetDataBase(mockDB)715	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)716	defer stubs.Reset()717	MockPaasAdminCheck(mockDB)718	netInfo := string(`{719    "network": {720        "cidr": "123.124.125.0/24",721        "gateway": "123.124.125.1",722        "name": "network_show",723        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"724    }}`)725	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(726		netInfo, nil)727	mockIaaS.EXPECT().DetachNetFromRouter(gomock.Any(),728		gomock.Any()).Return("", errors.New("error"))729	resp := DetachRouter("uuid-for-routet", "uuid-for-network")730	Convey("TestDetachERR406B\n", t, func() {731		So(resp.Code, ShouldEqual, 406)732	})733}734func TestDetachERR406C(t *testing.T) {735	cfgMock := gomock.NewController(t)736	defer cfgMock.Finish()737	mockDB := NewMockDbAccessor(cfgMock)738	mockIaaS := NewMockIaaS(cfgMock)739	common.SetDataBase(mockDB)740	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)741	defer stubs.Reset()742	netInfo := string(`{743    "network": {744        "cidr": "123.124.125.0/24",745        "gateway": "123.124.125.1",746        "name": "network_show",747        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"748    }}`)749	MockPaasAdminCheck(mockDB)750	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(751		netInfo, nil)752	mockIaaS.EXPECT().DetachNetFromRouter(gomock.Any(),753		gomock.Any()).Return("uuid-for-port", nil)754	mockDB.EXPECT().DeleteLeaf(755		gomock.Any()).Return(errors.New("error"))756	resp := DetachRouter("uuid-for-routet", "uuid-for-network")757	Convey("TestDetachERR406C\n", t, func() {758		So(resp.Code, ShouldEqual, 406)759	})760}761func TestDetachERR406D(t *testing.T) {762	cfgMock := gomock.NewController(t)763	defer cfgMock.Finish()764	mockDB := NewMockDbAccessor(cfgMock)765	mockIaaS := NewMockIaaS(cfgMock)766	common.SetDataBase(mockDB)767	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)768	defer stubs.Reset()769	netInfo := string(`{770    "network": {771        "cidr": "123.124.125.0/24",772        "gateway": "123.124.125.1",773        "name": "network_show",774        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"775    }}`)776	MockPaasAdminCheck(mockDB)777	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(778		netInfo, nil)779	mockIaaS.EXPECT().DetachNetFromRouter(gomock.Any(),780		gomock.Any()).Return("uuid-for-port", nil)781	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(nil)782	mockDB.EXPECT().DeleteLeaf(783		gomock.Any()).Return(errors.New("error"))784	resp := DetachRouter("uuid-for-routet", "uuid-for-network")785	Convey("TestDetachERR406D\n", t, func() {786		So(resp.Code, ShouldEqual, 406)787	})788}789func TestDetachERR406E(t *testing.T) {790	cfgMock := gomock.NewController(t)791	defer cfgMock.Finish()792	mockDB := NewMockDbAccessor(cfgMock)793	mockIaaS := NewMockIaaS(cfgMock)794	common.SetDataBase(mockDB)795	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)796	defer stubs.Reset()797	netInfo := string(`{798    "network": {799        "cidr": "123.124.125.0/24",800        "gateway": "123.124.125.1",801        "name": "network_show",802        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"803    }}`)804	MockPaasAdminCheck(mockDB)805	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(806		netInfo, nil)807	mockIaaS.EXPECT().DetachNetFromRouter(gomock.Any(),808		gomock.Any()).Return("uuid-for-port", nil)809	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(nil)810	mockDB.EXPECT().DeleteLeaf(gomock.Any()).Return(nil)811	mockDB.EXPECT().DeleteDir(gomock.Any()).Return(errors.New("error"))812	resp := DetachRouter("uuid-for-routet", "uuid-for-network")813	Convey("TestDetachERR406D\n", t, func() {814		So(resp.Code, ShouldEqual, 406)815	})816}817func TestDetachOK(t *testing.T) {818	cfgMock := gomock.NewController(t)819	defer cfgMock.Finish()820	mockDB := NewMockDbAccessor(cfgMock)821	mockIaaS := NewMockIaaS(cfgMock)822	common.SetDataBase(mockDB)823	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)824	defer stubs.Reset()825	netInfo := string(`{826    "network": {827        "cidr": "123.124.125.0/24",828        "gateway": "123.124.125.1",829        "name": "network_show",830        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"831    }}`)832	MockPaasAdminCheck(mockDB)...

Full Screen

Full Screen

ut_port_test.go

Source:ut_port_test.go Github

copy

Full Screen

...23	"github.com/ZTE/Knitter/pkg/iaas-accessor"24	"github.com/golang/gostub"25)26func TestApiCreatPortERR401(t *testing.T) {27	//cfgMock := gomock.NewController(t)28	//defer cfgMock.Finish()29	//mockDB := NewMockDbAccessor(cfgMock)30	//common.SetDataBase(mockDB)31	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)32	defer stubs.Reset()33	resp := APICreatePort("the-uuid-of-port")34	Convey("TestApiCreatPortERR401\n", t, func() {35		So(resp.Code, ShouldEqual, 401)36	})37}38func TestApiCreatPortERR403A(t *testing.T) {39	cfgMock := gomock.NewController(t)40	defer cfgMock.Finish()41	//mockDB := NewMockDbAccessor(cfgMock)42	mockIaaS := NewMockIaaS(cfgMock)43	//common.SetDataBase(mockDB)44	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)45	defer stubs.Reset()46	resp := APICreatePort("844d0d23-2d53-454a-93cf-73c8253f94d6")47	Convey("TestApiCreatPortERR403A\n", t, func() {48		So(resp.Code, ShouldEqual, 403)49	})50}51func TestApiCreatPortERR406B(t *testing.T) {52	cfgMock := gomock.NewController(t)53	defer cfgMock.Finish()54	//mockDB := NewMockDbAccessor(cfgMock)55	mockIaaS := NewMockIaaS(cfgMock)56	//common.SetDataBase(mockDB)57	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)58	defer stubs.Reset()59	cfg := string(`{"network_name":"want-to-attach-network",60		"vnic_type":"normal"}`)61	resp := APICreatePort(cfg)62	Convey("TestApiCreatPortERR406B\n", t, func() {63		So(resp.Code, ShouldEqual, 406)64	})65}66func listAllNetwork(mockDB *MockDbAccessor) {67	var list []*client.Node68	node := client.Node{Key: "network-uuid", Value: "network-info"}69	list = append(list, &node)70	list = append(list, &node)71	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)72	netInfo0 := string(`{73    "network": {74        "cidr": "123.124.125.0/24",75        "gateway": "123.124.125.1",76        "name": "want-to-attach-network",77        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"78    }}`)79	netInfo1 := string(`{80    "network": {81        "cidr": "123.124.125.0/24",82        "gateway": "123.124.125.1",83        "name": "network_not_show",84        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"85    }}`)86	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)87	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)88	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)89	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)90	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)91}92func listAllNetwork2(mockDB *MockDbAccessor) {93	var list []*client.Node94	node := client.Node{Key: "network-uuid", Value: "network-info"}95	list = append(list, &node)96	list = append(list, &node)97	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)98	netInfo0 := string(`{99    "network": {100        "cidr": "123.124.125.0/24",101        "gateway": "123.124.125.1",102        "name": "want-to-attach-network",103        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"104    }}`)105	netInfo1 := string(`{106    "network": {107        "cidr": "123.124.125.0/24",108        "gateway": "123.124.125.1",109        "name": "network_not_show",110        "network_id": "9a4bf247-0cdd-4649-a5aa-4255856d3da2"111    }}`)112	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)113	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)114	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)115	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)116	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)117	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)118	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)119	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)120	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)121	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)122	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)123	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)124	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)125	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)126	mockDB.EXPECT().ReadDir(gomock.Any()).Return(list, nil)127	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo0, nil)128	mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(netInfo1, nil)129}130func TestApiDeletePortERR401(t *testing.T) {131	cfgMock := gomock.NewController(t)132	defer cfgMock.Finish()133	//mockDB := NewMockDbAccessor(cfgMock)134	//common.SetDataBase(mockDB)135	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)136	defer stubs.Reset()137	resp := APIDeletePort("the-name-of-network")138	Convey("TestApiDeletePortERR401\n", t, func() {139		So(resp.Code, ShouldEqual, 401)140	})141}142func TestApiAttachERR401(t *testing.T) {143	cfgMock := gomock.NewController(t)144	defer cfgMock.Finish()145	//mockDB := NewMockDbAccessor(cfgMock)146	//common.SetDataBase(mockDB)147	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)148	defer stubs.Reset()149	resp := APIAttach("port-uuid", "vm-uuid")150	Convey("TestApiDeletePortERR401\n", t, func() {151		So(resp.Code, ShouldEqual, 401)152	})153}154func TestApiAttachERR404A(t *testing.T) {155	cfgMock := gomock.NewController(t)156	defer cfgMock.Finish()157	mockDB := NewMockDbAccessor(cfgMock)158	mockIaaS := NewMockIaaS(cfgMock)159	common.SetDataBase(mockDB)160	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)161	defer stubs.Reset()162	mockIaaS.EXPECT().AttachPortToVM(163		gomock.Any(), gomock.Any()).Return(nil, errors.New("error"))164	mockDB.EXPECT().SaveLeaf(gomock.Any(), gomock.Any())165	resp := APIAttach("port-uuid", "vm-uuid")166	Convey("TestApiAttachERR404A\n", t, func() {167		So(resp.Code, ShouldEqual, 406)168	})169}170func TestApiAttachERR404B(t *testing.T) {171	cfgMock := gomock.NewController(t)172	defer cfgMock.Finish()173	mockDB := NewMockDbAccessor(cfgMock)174	mockIaaS := NewMockIaaS(cfgMock)175	common.SetDataBase(mockDB)176	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)177	defer stubs.Reset()178	mockIaaS.EXPECT().AttachPortToVM(179		gomock.Any(), gomock.Any()).Return(nil, errors.New("error"))180	mockDB.EXPECT().SaveLeaf(gomock.Any(), gomock.Any()).Return(nil)181	resp := APIAttach("port-uuid", "vm-uuid")182	Convey("TestApiAttachERR404A\n", t, func() {183		So(resp.Code, ShouldEqual, 406)184	})185}186func TestApiAttachERR404C(t *testing.T) {187	cfgMock := gomock.NewController(t)188	defer cfgMock.Finish()189	mockDB := NewMockDbAccessor(cfgMock)190	mockIaaS := NewMockIaaS(cfgMock)191	common.SetDataBase(mockDB)192	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)193	defer stubs.Reset()194	mockIaaS.EXPECT().AttachPortToVM(gomock.Any(), gomock.Any()).Return(nil, nil)195	mockIaaS.EXPECT().GetType().Return("TECS")196	mockIaaS.EXPECT().GetPort(gomock.Any()).Return(nil, errors.New("error"))197	mockIaaS.EXPECT().DetachPortFromVM(gomock.Any(), gomock.Any()).Return(nil)198	resp := APIAttach("port-uuid", "vm-uuid")199	Convey("TestApiAttachERR404A\n", t, func() {200		So(resp.Code, ShouldEqual, 406)201	})202}203func TestApiAttachERR404D(t *testing.T) {204	cfgMock := gomock.NewController(t)205	defer cfgMock.Finish()206	mockDB := NewMockDbAccessor(cfgMock)207	mockIaaS := NewMockIaaS(cfgMock)208	common.SetDataBase(mockDB)209	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)210	defer stubs.Reset()211	mockIaaS.EXPECT().AttachPortToVM(gomock.Any(), gomock.Any()).Return(nil, nil)212	mockIaaS.EXPECT().GetType().Return("TECS")213	mockIaaS.EXPECT().GetPort(gomock.Any()).Return(nil, errors.New("error"))214	mockIaaS.EXPECT().DetachPortFromVM(gomock.Any(), gomock.Any()).Return(errors.New("error"))215	mockDB.EXPECT().SaveLeaf(gomock.Any(), gomock.Any()).Return(nil)216	resp := APIAttach("port-uuid", "vm-uuid")217	Convey("TestApiAttachERR404A\n", t, func() {218		So(resp.Code, ShouldEqual, 406)219	})220}221func TestApiAttachOK(t *testing.T) {222	cfgMock := gomock.NewController(t)223	defer cfgMock.Finish()224	mockDB := NewMockDbAccessor(cfgMock)225	mockIaaS := NewMockIaaS(cfgMock)226	common.SetDataBase(mockDB)227	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)228	defer stubs.Reset()229	mockIaaS.EXPECT().AttachPortToVM(gomock.Any(), gomock.Any()).Return(nil, nil)230	mockIaaS.EXPECT().GetType().Return("TECS")231	port := iaasaccessor.Interface{Status: "ACTIVE"}232	mockIaaS.EXPECT().GetPort(gomock.Any()).Return(&port, nil)233	resp := APIAttach("port-uuid", "vm-uuid")234	Convey("TestApiAttachERR404A\n", t, func() {235		So(resp.Code, ShouldEqual, 200)236	})237}238func TestApiAttachVNFMOK(t *testing.T) {239	cfgMock := gomock.NewController(t)240	defer cfgMock.Finish()241	mockDB := NewMockDbAccessor(cfgMock)242	mockIaaS := NewMockIaaS(cfgMock)243	common.SetDataBase(mockDB)244	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)245	defer stubs.Reset()246	mockIaaS.EXPECT().AttachPortToVM(gomock.Any(), gomock.Any()).Return(nil, nil)247	mockIaaS.EXPECT().GetType().Return("VNFM")248	resp := APIAttach("port-uuid", "vm-uuid")249	Convey("TestApiAttachVNFMOK\n", t, func() {250		So(resp.Code, ShouldEqual, 200)251	})252}253func TestApiDetachERR401(t *testing.T) {254	cfgMock := gomock.NewController(t)255	defer cfgMock.Finish()256	//mockDB := NewMockDbAccessor(cfgMock)257	//common.SetDataBase(mockDB)258	stubs := gostub.StubFunc(&iaas.GetIaaS, nil)259	defer stubs.Reset()260	resp := APIDetach("port-uuid", "vm-uuid")261	Convey("TestApiDetachERR401\n", t, func() {262		So(resp.Code, ShouldEqual, 401)263	})264}265func TestApiDetachERR404A(t *testing.T) {266	cfgMock := gomock.NewController(t)267	defer cfgMock.Finish()268	mockDB := NewMockDbAccessor(cfgMock)269	mockIaaS := NewMockIaaS(cfgMock)270	common.SetDataBase(mockDB)271	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)272	defer stubs.Reset()273	mockIaaS.EXPECT().DetachPortFromVM(274		gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(3)275	mockDB.EXPECT().SaveLeaf(gomock.Any(), gomock.Any()).Return(nil)276	resp := APIDetach("port-uuid", "vm-uuid")277	Convey("TestApiAttachERR404A\n", t, func() {278		So(resp.Code, ShouldEqual, 406)279	})280}281func TestApiDetachERR404B(t *testing.T) {282	cfgMock := gomock.NewController(t)283	defer cfgMock.Finish()284	mockDB := NewMockDbAccessor(cfgMock)285	mockIaaS := NewMockIaaS(cfgMock)286	common.SetDataBase(mockDB)287	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)288	defer stubs.Reset()289	mockIaaS.EXPECT().DetachPortFromVM(gomock.Any(), gomock.Any()).Return(nil)290	mockIaaS.EXPECT().GetType().Return("TECS")291	mockIaaS.EXPECT().GetPort(gomock.Any()).Return(nil, errors.New("error"))292	mockDB.EXPECT().SaveLeaf(gomock.Any(), gomock.Any()).Return(nil)293	resp := APIDetach("port-uuid", "vm-uuid")294	Convey("TestApiAttachERR404A\n", t, func() {295		So(resp.Code, ShouldEqual, 406)296	})297}298func TestApiDetachOK(t *testing.T) {299	cfgMock := gomock.NewController(t)300	defer cfgMock.Finish()301	mockDB := NewMockDbAccessor(cfgMock)302	mockIaaS := NewMockIaaS(cfgMock)303	common.SetDataBase(mockDB)304	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)305	defer stubs.Reset()306	mockIaaS.EXPECT().DetachPortFromVM(gomock.Any(), gomock.Any()).Return(nil)307	mockIaaS.EXPECT().GetType().Return("TECS")308	port2 := iaasaccessor.Interface{Status: "DOWN"}309	mockIaaS.EXPECT().GetPort(gomock.Any()).Return(&port2, nil)310	resp := APIDetach("port-uuid", "vm-uuid")311	Convey("TestApiAttachERR404A\n", t, func() {312		So(resp.Code, ShouldEqual, 200)313	})314}315func TestApiDetachVNFMOK(t *testing.T) {316	cfgMock := gomock.NewController(t)317	defer cfgMock.Finish()318	mockDB := NewMockDbAccessor(cfgMock)319	mockIaaS := NewMockIaaS(cfgMock)320	common.SetDataBase(mockDB)321	stubs := gostub.StubFunc(&iaas.GetIaaS, mockIaaS)322	defer stubs.Reset()323	mockIaaS.EXPECT().DetachPortFromVM(gomock.Any(), gomock.Any()).Return(nil)324	mockIaaS.EXPECT().GetType().Return("VNFM")325	resp := APIDetach("port-uuid", "vm-uuid")326	Convey("TestApiAttachERR404A\n", t, func() {327		So(resp.Code, ShouldEqual, 200)328	})329}...

Full Screen

Full Screen

database_test.go

Source:database_test.go Github

copy

Full Screen

...21	"testing"22)23func TestCase1GetPaasUUID(t *testing.T) {24	var wantUUID string = "UUID-PAAS-KNITTER"25	mockCtl := gomock.NewController(t)26	defer mockCtl.Finish()27	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)28	SetDataBase(mockDB)29	gomock.InOrder(30		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return(wantUUID, nil),31	)32	convey.Convey("TestCase1GetPaasUUID\n", t, func() {33		id := GetPaasUUID()34		convey.So(id, convey.ShouldEqual, wantUUID)35	})36}37func TestCase2GetPaasUUID(t *testing.T) {38	mockCtl := gomock.NewController(t)39	defer mockCtl.Finish()40	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)41	SetDataBase(mockDB)42	gomock.InOrder(43		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",44			errors.New("unknow errors")),45	)46	convey.Convey("TestCase2GetPaasUUID\n", t, func() {47		id := GetPaasUUID()48		convey.So(id, convey.ShouldEqual, uuid.NIL.String())49	})50}51func TestCase3GetPaasUUID(t *testing.T) {52	mockCtl := gomock.NewController(t)53	defer mockCtl.Finish()54	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)55	SetDataBase(mockDB)56	gomock.InOrder(57		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",58			errors.New(ErrorKeyNotFound)),59		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfPaasUUID(),60			gomock.Any()).Return(nil),61	)62	convey.Convey("TestCase3GetPaasUUID\n", t, func() {63		id := GetPaasUUID()64		convey.So(id, convey.ShouldNotEqual, uuid.NIL.String())65	})66}67func TestCase4GetPaasUUID(t *testing.T) {68	mockCtl := gomock.NewController(t)69	defer mockCtl.Finish()70	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)71	SetDataBase(mockDB)72	gomock.InOrder(73		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",74			errors.New(ErrorKeyNotFound)),75		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfPaasUUID(),76			gomock.Any()).Return(errors.New("unknow error happen")),77	)78	convey.Convey("TestCase4GetPaasUUID\n", t, func() {79		id := GetPaasUUID()80		convey.So(id, convey.ShouldEqual, uuid.NIL.String())81	})82}83func TestCase1GetOpenstackCfg(t *testing.T) {84	mockCtl := gomock.NewController(t)85	defer mockCtl.Finish()86	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)87	SetDataBase(mockDB)88	gomock.InOrder(89		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfOpenstack()).Return("",90			errors.New(ErrorKeyNotFound)),91	)92	convey.Convey("TestCase1GetOpenstackCfg\n", t, func() {93		cfg := GetOpenstackCfg()94		convey.So(cfg, convey.ShouldEqual, "")95	})96}97func TestCase2GetOpenstackCfg(t *testing.T) {98	const OpenstackConfig string = "right openstack configuration"99	mockCtl := gomock.NewController(t)100	defer mockCtl.Finish()101	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)102	SetDataBase(mockDB)103	gomock.InOrder(104		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfOpenstack()).Return(105			OpenstackConfig, nil),106	)107	convey.Convey("TestCase2GetOpenstackCfg\n", t, func() {108		cfg := GetOpenstackCfg()109		convey.So(cfg, convey.ShouldEqual, OpenstackConfig)110	})111}112func TestCase1GetVnfmCfg(t *testing.T) {113	mockCtl := gomock.NewController(t)114	defer mockCtl.Finish()115	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)116	SetDataBase(mockDB)117	gomock.InOrder(118		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfVnfm()).Return(119			"", errors.New(ErrorKeyNotFound)),120	)121	convey.Convey("TestCase1GetVnfmCfg\n", t, func() {122		cfg := GetVnfmCfg()123		convey.So(cfg, convey.ShouldEqual, "")124	})125}126func TestCase2GetVnfmCfg(t *testing.T) {127	const OpenstackConfig string = "right openstack configuration"128	mockCtl := gomock.NewController(t)129	defer mockCtl.Finish()130	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)131	SetDataBase(mockDB)132	gomock.InOrder(133		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfVnfm()).Return(134			OpenstackConfig, nil),135	)136	convey.Convey("TestCase2GetVnfmCfg\n", t, func() {137		cfg := GetVnfmCfg()138		convey.So(cfg, convey.ShouldEqual, OpenstackConfig)139	})140}141func TestCase1RegisterSelfToDb(t *testing.T) {142	const ManagerURL string = "https://paas.zte.com.cn:9527"143	mockCtl := gomock.NewController(t)144	defer mockCtl.Finish()145	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)146	SetDataBase(mockDB)147	gomock.InOrder(148		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfKnitterManagerUrl(),149			ManagerURL).Return(nil),150	)151	convey.Convey("TestCase1RegisterSelfToDb\n", t, func() {152		e := RegisterSelfToDb(ManagerURL)153		convey.So(e, convey.ShouldEqual, nil)154	})155}156func TestCase2RegisterSelfToDb(t *testing.T) {157	const ManagerURL string = "https://paas.zte.com.cn:9527"158	mockCtl := gomock.NewController(t)159	defer mockCtl.Finish()160	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)161	SetDataBase(mockDB)162	gomock.InOrder(163		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfKnitterManagerUrl(),164			ManagerURL).Return(errors.New(ErrorKeyNotFound)),165	)166	convey.Convey("TestCase1RegisterSelfToDb\n", t, func() {167		e := RegisterSelfToDb(ManagerURL)168		convey.So(e, convey.ShouldNotEqual, nil)169	})170}171func TestCase1SetDataBase(t *testing.T) {172	convey.Convey("TestCase1SetDataBase\n", t, func() {173		e := SetDataBase(nil)174		convey.So(e, convey.ShouldEqual, nil)175	})176}177func TestCase1GetDataBase(t *testing.T) {178	convey.Convey("TestCase1GetDataBase\n", t, func() {179		e := SetDataBase(nil)180		convey.So(e, convey.ShouldEqual, nil)181		p := GetDataBase()182		convey.So(p, convey.ShouldEqual, nil)183	})184}185//todo fix ut error186/*func TestCase1CheckDB(t *testing.T) {187	mockCtl := gomock.NewController(t)188	defer mockCtl.Finish()189	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)190	SetDataBase(mockDB)191	gomock.InOrder(192		//case 1193		mockDB.EXPECT().SaveLeaf(gomock.Any(),194			gomock.Any()).Return(errors.New(ErrorKeyNotFound)),195		//case 2196		mockDB.EXPECT().SaveLeaf(gomock.Any(),197			gomock.Any()).Return(nil),198		mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(199			"", errors.New(ErrorKeyNotFound)),200		//case 3201		mockDB.EXPECT().SaveLeaf(gomock.Any(),...

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1mockCtrl := gomock.NewController(t)2defer mockCtrl.Finish()3mockCtrl := gomock.NewController(t)4defer mockCtrl.Finish()5mockCtrl := gomock.NewController(t)6defer mockCtrl.Finish()7--- FAIL: TestNewController (0.00s)8panic: gomock: out of order call: *mocks.MockController.Finish() [recovered]9    panic: gomock: out of order call: *mocks.MockController.Finish()10testing.tRunner.func1(0xc4200d40f0)11panic(0x11c9e60, 0xc42000a4e0)12github.com/golang/mock/gomock.(*Controller).Finish(0xc4200b2000)13github.com/golang/mock/gomock.NewController(0x0, 0x0, 0x0, 0x0)14github.com/golang/mock/gomock.NewController-fm(0x0, 0x0, 0x0, 0x0)15github.com/golang/mock/gomock.(*Controller).Finish(0xc4200b2000)16github.com/golang/mock/gomock.NewController(0x

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1func NewController(t TestingT) *gomock.Controller {2	return gomock.NewController(t)3}4func NewController(t TestingT) *gomock.Controller {5	return gomock.NewController(t)6}7So, I have to import the 1.go file in my test file. I have to import the 1.go file in my test file. I have to import the 1.go file in my test file. I have t

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1import (2func TestController(t *testing.T) {3    ctrl := gomock.NewController(t)4    defer ctrl.Finish()5    fmt.Println("Test Controller")6}7import (8func TestController(t *testing.T) {9    ctrl := gomock.NewController(t)10    defer ctrl.Finish()11    fmt.Println("Test Controller")12}

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1import (2func TestNewController(t *testing.T) {3	ctrl := gomock.NewController(t)4	defer ctrl.Finish()5	fmt.Println(ctrl)6	assert.NotNil(t, ctrl)7}8import (9func TestNewController(t *testing.T) {10	ctrl := gomock.NewController(t)11	defer ctrl.Finish()12	fmt.Println(ctrl)13	assert.NotNil(t, ctrl)14}15import (16func TestNewController(t *testing.T) {17	ctrl := gomock.NewController(t)18	defer ctrl.Finish()19	fmt.Println(ctrl)20	assert.NotNil(t, ctrl)21}22import (23func TestNewController(t *testing.T) {24	ctrl := gomock.NewController(t)25	defer ctrl.Finish()26	fmt.Println(ctrl)27	assert.NotNil(t, ctrl)28}29import (30func TestNewController(t *testing.T) {31	ctrl := gomock.NewController(t)32	defer ctrl.Finish()33	fmt.Println(ctrl)34	assert.NotNil(t, ctrl)35}36import (37func TestNewController(t *testing.T) {38	ctrl := gomock.NewController(t)39	defer ctrl.Finish()40	fmt.Println(ctrl)41	assert.NotNil(t, ctrl)42}

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1func TestController(t *testing.T) {2	ctrl := gomock.NewController(t)3	defer ctrl.Finish()4}5func TestController(t *testing.T) {6	ctrl := gomock.NewController(t)7	defer ctrl.Finish()8}9func TestController(t *testing.T) {10	ctrl := gomock.NewController(t)11	defer ctrl.Finish()12}13func TestController(t *testing.T) {14	ctrl := gomock.NewController(t)15	defer ctrl.Finish()16}17func TestController(t *testing.T) {18	ctrl := gomock.NewController(t)19	defer ctrl.Finish()20}21func TestController(t *testing.T) {22	ctrl := gomock.NewController(t)23	defer ctrl.Finish()24}25func TestController(t *testing.T) {26	ctrl := gomock.NewController(t)27	defer ctrl.Finish()28}29func TestController(t *testing.T) {30	ctrl := gomock.NewController(t)31	defer ctrl.Finish()32}33func TestController(t *testing.T) {34	ctrl := gomock.NewController(t)35	defer ctrl.Finish()36}

Full Screen

Full Screen

NewController

Using AI Code Generation

copy

Full Screen

1func TestNewController(t *testing.T) {2    mock := NewMockMyInterface(ctrl)3    c := NewController(t)4}5func TestNewController(t *testing.T) {6    mock := NewMockMyInterface(ctrl)7    c := NewController(t)8}9func TestNewController(t *testing.T) {10    mock := NewMockMyInterface(ctrl)11    c := NewController(t)12}13func TestNewController(t *testing.T) {14    mock := NewMockMyInterface(ctrl)15    c := NewController(t)16}17func TestNewController(t *testing.T) {18    mock := NewMockMyInterface(ctrl)19    c := NewController(t)20}21func TestNewController(t *testing.T) {22    mock := NewMockMyInterface(ctrl)23    c := NewController(t)24}25func TestNewController(t *testing.T) {26    mock := NewMockMyInterface(ctrl)27    c := NewController(t)28}29func TestNewController(t *testing.T) {30    mock := NewMockMyInterface(ctrl)31    c := NewController(t

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful