// startReading starts a goroutine receiving the lines out of the reader // in the background and passing them to a created string channel. This // will used in the assertions. func startReading(c *gc.C, tailer *tailer.Tailer, reader *io.PipeReader, writer *io.PipeWriter) chan string { linec := make(chan string) // Start goroutine for reading. go func() { defer close(linec) reader := bufio.NewReader(reader) for { line, err := reader.ReadString('\n') switch err { case nil: linec <- line case io.EOF: return default: c.Fail() } } }() // Close writer when tailer is stopped or has an error. Tailer using // components can do it the same way. go func() { tailer.Wait() writer.Close() }() return linec }
func (s *BootstrapS) TestFailedAndSucceed(c *gocheck.C) { c.Fail() c.Succeed() if c.Failed() { critical("c.Succeed() didn't put the test back in a non-failed state") } }
func checkToolsContent(c *gc.C, data []byte, uploaded string) { zr, err := gzip.NewReader(bytes.NewReader(data)) c.Check(err, gc.IsNil) defer zr.Close() tr := tar.NewReader(zr) found := false for { hdr, err := tr.Next() if err == io.EOF { break } c.Check(err, gc.IsNil) if strings.ContainsAny(hdr.Name, "/\\") { c.Fail() } if hdr.Typeflag != tar.TypeReg { c.Fail() } content, err := ioutil.ReadAll(tr) c.Check(err, gc.IsNil) c.Check(string(content), gc.Equals, uploaded) found = true } c.Check(found, jc.IsTrue) }
func (s *IntegrationTestSuite) TestLongContainerName(c *chk.C) { id, err := containers.NewIdentifier("IntTest006xxxxxxxxxxxxxx") c.Assert(err, chk.IsNil) s.containerIds = append(s.containerIds, id) hostContainerId := fmt.Sprintf("%v/%v", s.daemonURI, id) cmd := exec.Command("/usr/bin/gear", "install", TestImage, hostContainerId, "--start", "--ports=8080:4003", "--isolate") data, err := cmd.CombinedOutput() c.Log(string(data)) c.Assert(err, chk.IsNil) s.assertContainerState(c, id, CONTAINER_STARTED) s.assertFilePresent(c, id.UnitPathFor(), 0664, true) s.assertFilePresent(c, filepath.Join(id.HomePath(), "container-init.sh"), 0700, false) ports, err := containers.GetExistingPorts(id) c.Assert(err, chk.IsNil) c.Assert(len(ports), chk.Equals, 1) t := time.NewTicker(time.Second / 10) defer t.Stop() select { case <-t.C: resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v", ports[0].External)) if err == nil { c.Assert(resp.StatusCode, chk.Equals, 200) } case <-time.After(time.Second * 15): c.Fail() } }
func (s *BootstrapS) TestFailedAndFail(c *gocheck.C) { if c.Failed() { critical("c.Failed() must be false first!") } c.Fail() if !c.Failed() { critical("c.Fail() didn't put the test in a failed state!") } c.Succeed() }
func (s *FoundationS) TestSucceedNow(c *gocheck.C) { defer (func() { if c.Failed() { c.Error("SucceedNow() didn't succeed the test") } if c.GetTestLog() != "" { c.Error("Something got logged:\n" + c.GetTestLog()) } })() c.Fail() c.SucceedNow() c.Log("SucceedNow() didn't stop the test") }
func (s *S) SetUpSuite(c *gocheck.C) { var err error config.Set("database:url", "127.0.0.1:27017") config.Set("database:name", "tsuru_service_test") s.conn, err = db.Conn() c.Assert(err, gocheck.IsNil) s.user = &auth.User{Email: "*****@*****.**"} err = s.user.Create() c.Assert(err, gocheck.IsNil) s.team = &auth.Team{Name: "Raul", Users: []string{s.user.Email}} err = s.conn.Teams().Insert(s.team) c.Assert(err, gocheck.IsNil) if err != nil { c.Fail() } }
func (s *FoundationS) TestErrorf(c *gocheck.C) { // Do not use checkState() here. It depends on Errorf() working. expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+ " c.Errorf(\"Error %%v!\", \"message\")\n"+ "... Error: Error message!\n\n", getMyLine()+1) c.Errorf("Error %v!", "message") failed := c.Failed() c.Succeed() if log := c.GetTestLog(); log != expectedLog { c.Logf("Errorf() logged %#v rather than %#v", log, expectedLog) c.Fail() } if !failed { c.Logf("Errorf() didn't put the test in a failed state") c.Fail() } }
func (sts *SettingTestSuite) SetUpTest(c *C.C) { var err error core := NewMockSettingCore() sts.s, err = NewSetting(core) sts.CategoryDisplayModeChangedCount = 0 sts.s.CategoryDisplayModeChanged = func(int64) { sts.CategoryDisplayModeChangedCount++ } sts.SortMethodChangedCount = 0 sts.s.SortMethodChanged = func(int64) { sts.SortMethodChangedCount++ } if err != nil { c.Fail() } }
func (self *PinYinTestSuite) TestPinYin(c *C.C) { names := []string{} oldLang := os.Getenv("LANGUAGE") os.Setenv("LANGUAGE", "zh_CN.UTF-8") addName := func(m *[]string, n string) { app := gio.NewDesktopAppInfoFromFilename(n) if app == nil { c.Skip("create desktop app info failed") return } defer app.Unref() name := app.GetDisplayName() c.Logf("add %q to names", name) *m = append(*m, name) } addName(&names, path.Join(self.testDataDir, "deepin-software-center.desktop")) addName(&names, path.Join(self.testDataDir, "firefox.desktop")) tree, err := NewPinYinSearchAdapter(names) if err != nil { c.Log(err) c.Fail() } search := func(key string, res []string) { keys, err := tree.Search(key) if err != nil { c.Log(err) c.Fail() return } c.Assert(keys, C.DeepEquals, res) } search("shang", []string{"深度商店"}) search("sd", []string{"深度商店"}) search("商店", []string{"深度商店"}) search("firefox", []string{"Firefox 网络浏览器"}) search("wang", []string{"Firefox 网络浏览器"}) search("网络", []string{"Firefox 网络浏览器"}) os.Setenv("LANGUAGE", oldLang) }
func (s *clientSuite) TestShareEnvironmentThreeUsers(c *gc.C) { client := s.APIState.Client() existingUser := s.Factory.MakeEnvUser(c, nil) localUser := s.Factory.MakeUser(c, nil) newUserTag := names.NewUserTag("foo@bar") cleanup := api.PatchClientFacadeCall(client, func(request string, paramsIn interface{}, response interface{}) error { if users, ok := paramsIn.(params.ModifyEnvironUsers); ok { c.Assert(users.Changes, gc.HasLen, 3) c.Logf(string(users.Changes[0].Action), gc.Equals, string(params.AddEnvUser)) c.Logf(users.Changes[0].UserTag, gc.Equals, existingUser.UserTag().String()) c.Logf(string(users.Changes[1].Action), gc.Equals, string(params.AddEnvUser)) c.Logf(users.Changes[1].UserTag, gc.Equals, localUser.UserTag().String()) c.Logf(string(users.Changes[1].Action), gc.Equals, string(params.AddEnvUser)) c.Logf(users.Changes[1].UserTag, gc.Equals, newUserTag.String()) } else { c.Log("wrong input structure") c.Fail() } if result, ok := response.(*params.ErrorResults); ok { err := ¶ms.Error{Message: "failed to create environment user: env user already exists"} *result = params.ErrorResults{Results: []params.ErrorResult{{Error: err}, {Error: nil}, {Error: nil}}} } else { c.Log("wrong output structure") c.Fail() } return nil }, ) defer cleanup() result, err := client.ShareEnvironment([]names.UserTag{existingUser.UserTag(), localUser.UserTag(), newUserTag}) c.Assert(err, gc.IsNil) c.Assert(result.Results, gc.HasLen, 3) c.Assert(result.Results[0].Error, gc.ErrorMatches, `failed to create environment user: env user already exists`) c.Assert(result.Results[1].Error, gc.IsNil) c.Assert(result.Results[2].Error, gc.IsNil) }
func (s *FailHelper) TestLogAndFail(c *gocheck.C) { s.testLine = getMyLine() - 1 c.Log("Expected failure!") c.Fail() }