func TestGetPubKey(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) doPub := def.NowDo() doPub.Address = testsGenAKey() pub := new(bytes.Buffer) config.GlobalConfig.Writer = pub if err := GetPubKey(doPub); err != nil { fatal(t, err) } pubBytes := pub.Bytes() pubkey := util.TrimString(string(pubBytes)) key := new(bytes.Buffer) config.GlobalConfig.Writer = key doKey := def.NowDo() doKey.Address = doPub.Address if err := ConvertKey(doKey); err != nil { fatal(t, err) } converted := regexp.MustCompile(`"pub_key":\[1,"([^"]+)"\]`).FindStringSubmatch(key.String())[1] if converted != pubkey { fatal(t, fmt.Errorf("Expected (%s), got (%s)\n", pubkey, converted)) } }
func TestGenerateKey(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) address := testsGenAKey() lsOut := new(bytes.Buffer) config.GlobalConfig.Writer = lsOut do := def.NowDo() do.Name = "keys" do.Operations.Interactive = false do.Operations.ContainerNumber = 1 path := path.Join(ErisContainerRoot, "keys", "data") do.Operations.Args = []string{"ls", path} if err := srv.ExecService(do); err != nil { fatal(t, err) } lsOutBytes := lsOut.Bytes() output := util.TrimString(string(lsOutBytes)) if address != output { fatal(t, fmt.Errorf("Expected (%s), got (%s)\n", address, output)) } }
//returns an addr for tests func testsGenAKey() string { addr := new(bytes.Buffer) config.GlobalConfig.Writer = addr doGen := def.NowDo() tests.IfExit(GenerateKey(doGen)) addrBytes := addr.Bytes() address := util.TrimString(string(addrBytes)) return address }
func TestExportKeySingle(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) //or some clean function address := testsGenAKey() catOut := new(bytes.Buffer) config.GlobalConfig.Writer = catOut do := def.NowDo() do.Name = "keys" do.Operations.Interactive = false do.Operations.ContainerNumber = 1 keyPath := path.Join(ErisContainerRoot, "keys", "data", address, address) //cat container contents of new key do.Operations.Args = []string{"cat", keyPath} if err := srv.ExecService(do); err != nil { fatal(t, err) } catOutBytes := catOut.Bytes() keyInCont := util.TrimString(string(catOutBytes)) doExp := def.NowDo() doExp.Address = address doExp.Destination = filepath.Join(KeysPath, "data") //is default //export if err := ExportKey(doExp); err != nil { fatal(t, err) } //cat host contents key, err := ioutil.ReadFile(filepath.Join(doExp.Destination, address, address)) if err != nil { fatal(t, err) } keyOnHost := util.TrimString(string(key)) if keyInCont != keyOnHost { fatal(t, fmt.Errorf("Expected (%s), got (%s)\n", keyInCont, keyOnHost)) } }
func ListKeys(do *definitions.Do) error { if do.Host { keysPath := filepath.Join(KeysPath, "data") addrs, err := ioutil.ReadDir(keysPath) if err != nil { return err } hostAddrs := make([]string, len(addrs)) for i, addr := range addrs { hostAddrs[i] = addr.Name() } do.Result = strings.Join(hostAddrs, ",") log.WithField("=>", hostAddrs[0]).Warn("The keys on your host kind marmot:") hostAddrs = append(hostAddrs[:0], hostAddrs[1:]...) for _, addr := range hostAddrs { log.WithField("=>", addr).Warn() } } if do.Container { keysOut := new(bytes.Buffer) config.GlobalConfig.Writer = keysOut do.Name = "keys" do.Operations.ContainerNumber = 1 if err := srv.EnsureRunning(do); err != nil { return err } do.Operations.Interactive = false do.Operations.Args = []string{"ls", "/home/eris/.eris/keys/data"} if err := srv.ExecService(do); err != nil { return err } keysOutString := strings.Split(util.TrimString(string(keysOut.Bytes())), "\n") do.Result = strings.Join(keysOutString, ",") log.WithField("=>", keysOutString[0]).Warn("The keys in your container kind marmot:") keysOutString = append(keysOutString[:0], keysOutString[1:]...) for _, addr := range keysOutString { log.WithField("=>", addr).Warn() } } return nil }
func TestListDataContainers(t *testing.T) { dataName1 := fmt.Sprintf("%s%s", dataName, "one") dataName2 := fmt.Sprintf("%s%s", dataName, "two") datas := make(map[string]bool) datas[dataName] = true datas[dataName1] = true datas[dataName2] = true testCreateDataByImport(t, dataName) testCreateDataByImport(t, dataName1) testCreateDataByImport(t, dataName2) defer testKillDataCont(t, dataName) defer testKillDataCont(t, dataName1) defer testKillDataCont(t, dataName2) do := definitions.NowDo() do.Quiet = true if err := list.ListDatas(do); err != nil { log.Error(err) t.FailNow() } output := strings.Split(do.Result, "\n") i := 0 for _, out := range output { if datas[util.TrimString(out)] == true { i++ } } if i != 3 { log.Error(fmt.Errorf("Expected 3 data containers, got (%v)\n", i)) t.Fail() } }
func TestListKeyContainer(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) addrs := make(map[string]bool) addrs[testsGenAKey()] = true addrs[testsGenAKey()] = true addrs[testsGenAKey()] = true output := testListKeys("container") i := 0 for _, out := range output { if addrs[util.TrimString(out)] == true { i++ } } if i != 3 { fatal(t, fmt.Errorf("Expected 3 keys, got (%v)\n", i)) } }
func TestListKeyHost(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) addr0 := testsGenAKey() addr1 := testsGenAKey() addrs := make(map[string]bool) addrs[addr0] = true addrs[addr1] = true doExp := def.NowDo() doExp.Address = addr0 doExp.Destination = filepath.Join(KeysPath, "data") //is default if err := ExportKey(doExp); err != nil { fatal(t, err) } doExp.Address = addr1 if err := ExportKey(doExp); err != nil { fatal(t, err) } output := testListKeys("host") i := 0 for _, out := range output { if addrs[util.TrimString(out)] == true { i++ } } if i != 2 { fatal(t, fmt.Errorf("Expected 2 keys, got (%v)\n", i)) } }
func TestImportKeySingle(t *testing.T) { testStartKeys(t) defer testKillService(t, "keys", true) address := testsGenAKey() //export it doExp := def.NowDo() doExp.Address = address doExp.Destination = filepath.Join(KeysPath, "data") //is default set by flag if err := ExportKey(doExp); err != nil { fatal(t, err) } key, err := ioutil.ReadFile(filepath.Join(doExp.Destination, address, address)) if err != nil { fatal(t, err) } //key b4 import keyOnHost := util.TrimString(string(key)) //rm key that was generated before import doRm := def.NowDo() doRm.Name = "keys" doRm.Operations.Interactive = false doRm.Operations.ContainerNumber = 1 keyPath := path.Join(ErisContainerRoot, "keys", "data", address) doRm.Operations.Args = []string{"rm", "-rf", keyPath} if err := srv.ExecService(doRm); err != nil { fatal(t, err) } doImp := def.NowDo() doImp.Address = address //doImp.Destination // set in function doImp.Source = filepath.Join(KeysPath, "data") if err := ImportKey(doImp); err != nil { fatal(t, err) } catOut := new(bytes.Buffer) config.GlobalConfig.Writer = catOut doCat := def.NowDo() doCat.Name = "keys" doCat.Operations.Interactive = false doCat.Operations.ContainerNumber = 1 keyPathCat := path.Join(ErisContainerRoot, "keys", "data", address, address) //cat container contents of new key doCat.Operations.Args = []string{"cat", keyPathCat} if err := srv.ExecService(doCat); err != nil { fatal(t, err) } catOutBytes := catOut.Bytes() keyInCont := util.TrimString(string(catOutBytes)) if keyOnHost != keyInCont { fatal(t, fmt.Errorf("Expected (%s), got (%s)\n", keyOnHost, keyInCont)) } }