|
| 1 | +package dockplugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/contiv/netplugin/core" |
| 6 | + "github.com/contiv/netplugin/netmaster/docknet" |
| 7 | + "github.com/contiv/netplugin/netmaster/mastercfg" |
| 8 | + "github.com/contiv/netplugin/state" |
| 9 | + "github.com/contiv/netplugin/utils" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +var stateDriver *state.FakeStateDriver |
| 14 | + |
| 15 | +// initStateDriver initialize etcd state driver |
| 16 | +func initFakeStateDriver(t *testing.T) { |
| 17 | + instInfo := core.InstanceInfo{} |
| 18 | + d, err := utils.NewStateDriver("fakedriver", &instInfo) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("failed to init statedriver. Error: %s", err) |
| 21 | + t.Fail() |
| 22 | + } |
| 23 | + stateDriver = d.(*state.FakeStateDriver) |
| 24 | +} |
| 25 | + |
| 26 | +func deinitStateDriver() { |
| 27 | + utils.ReleaseStateDriver() |
| 28 | +} |
| 29 | + |
| 30 | +// Ensure deleteNetworkHelper can delete docker network without issue |
| 31 | +func TestCreateAndDeleteNetwork(t *testing.T) { |
| 32 | + // Update plugin driver for unit test |
| 33 | + docknet.UpdateDockerV2PluginName("bridge", "default") |
| 34 | + |
| 35 | + initFakeStateDriver(t) |
| 36 | + defer deinitStateDriver() |
| 37 | + |
| 38 | + tenantName := "t1" |
| 39 | + networkName := "net1" |
| 40 | + serviceName := "" |
| 41 | + nwcfg := mastercfg.CfgNetworkState{ |
| 42 | + Tenant: tenantName, |
| 43 | + NetworkName: networkName, |
| 44 | + PktTagType: "vlan", |
| 45 | + PktTag: 1, |
| 46 | + ExtPktTag: 1, |
| 47 | + SubnetIP: "10.0.0.0", |
| 48 | + SubnetLen: 24, |
| 49 | + Gateway: "10.0.0.1", |
| 50 | + } |
| 51 | + |
| 52 | + err := docknet.CreateDockNet(tenantName, networkName, "", &nwcfg) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Error creating docker network. Error: %v", err) |
| 55 | + t.Fail() |
| 56 | + } |
| 57 | + |
| 58 | + // Get Docker network UUID |
| 59 | + dnetOper := docknet.DnetOperState{} |
| 60 | + dnetOper.StateDriver = stateDriver |
| 61 | + |
| 62 | + err = dnetOper.Read(fmt.Sprintf("%s.%s.%s", tenantName, networkName, serviceName)) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Unable to read network state. Error: %v", err) |
| 65 | + t.Fail() |
| 66 | + } |
| 67 | + |
| 68 | + testData := make([]byte, 3) |
| 69 | + networkID := mastercfg.StateConfigPath + "nets/" + dnetOper.DocknetUUID + ".default" |
| 70 | + dnetOper.StateDriver.Write(networkID, testData) |
| 71 | + |
| 72 | + // Delete Docker network by using helper |
| 73 | + err = deleteNetworkHelper(dnetOper.DocknetUUID) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("Unable to delete docker network. Error: %v", err) |
| 77 | + t.Fail() |
| 78 | + } |
| 79 | + |
| 80 | + // ensure network is not in the oper state |
| 81 | + err = dnetOper.Read(fmt.Sprintf("%s.%s.%s", tenantName, networkName, serviceName)) |
| 82 | + if err == nil { |
| 83 | + t.Fatalf("Fail to delete docket network") |
| 84 | + t.Fail() |
| 85 | + } |
| 86 | +} |
0 commit comments