<commit_before>package main

import (
	"fmt"
	"time"

	check "gopkg.in/check.v1"

	"github.com/Dataman-Cloud/swan/types"
)

func (s *ApiSuite) TestCanaryUpdate(c *check.C) {
	// purge

	err := s.purge(time.Second*30, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() purged")

	// create app
	ver := demoVersion().setName("demo").setCount(5).setCPU(0.01).setMem(5).Get()
	id := s.createApp(ver, c)
	err = s.waitApp(id, types.OpStatusNoop, time.Second*30, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() created")

	// verify app
	app := s.inspectApp(id, c)
	c.Assert(app.Name, check.Equals, "demo")
	c.Assert(app.TaskCount, check.Equals, 5)
	c.Assert(app.VersionCount, check.Equals, 1)
	c.Assert(len(app.Version), check.Equals, 1)

	// do canary update

	body := &types.CanaryUpdateBody{
		Version:   demoVersion().setName("demo").setCount(5).setCPU(0.01).setMem(10).Get(),
		Instances: 3,
		Value:     0.5,
		OnFailure: "continue",
		Delay:     0.5,
	}
	s.canaryUpdate(id, body, c)
	err = s.waitApp(id, types.OpStatusCanaryUnfinished, time.Second*180, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() updated")

	// verify app
	app = s.inspectApp(id, c)
	c.Assert(app.Name, check.Equals, "demo")
	c.Assert(app.VersionCount, check.Equals, 2)
	c.Assert(len(app.Version), check.Equals, 2)
	c.Assert(app.OpStatus, check.Equals, types.OpStatusCanaryUnfinished)

	// verify app tasks
	tasks := s.listAppTasks(id, c)
	c.Assert(len(tasks), check.Equals, 5)

	var n, m int
	for _, task := range tasks {
		if task.Weight == 67 {
			n++
		}

		if task.Weight == 100 {
			m++
		}
	}

	c.Assert(n, check.Equals, 3)
	c.Assert(m, check.Equals, 2)

	// verify app versions
	vers := s.listAppVersions(id, c)
	c.Assert(len(vers), check.Equals, 2)
	c.Assert(vers[0].Mem, check.Equals, float64(10))

	counter := make(map[string]int)
	for _, task := range tasks {
		if v, ok := counter[task.Version]; ok {
			v++
			counter[task.Version] = v
		} else {
			counter[task.Version] = 1
		}
	}

	c.Assert(counter[vers[0].ID], check.Equals, 3)
	c.Assert(counter[vers[1].ID], check.Equals, 2)

	// clean up

	err = s.removeApp(id, time.Second*10, c)
	c.Assert(err, check.IsNil)<commit_msg>more ci test for canary update<commit_after>package main

import (
	"fmt"
	"time"

	check "gopkg.in/check.v1"

	"github.com/Dataman-Cloud/swan/types"
)

func (s *ApiSuite) TestCanaryUpdate(c *check.C) {
	// purge

	err := s.purge(time.Second*30, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() purged")

	// create app
	ver := demoVersion().setName("demo").setCount(5).setCPU(0.01).setMem(5).Get()
	id := s.createApp(ver, c)
	err = s.waitApp(id, types.OpStatusNoop, time.Second*30, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() created")

	// verify app
	app := s.inspectApp(id, c)
	c.Assert(app.Name, check.Equals, "demo")
	c.Assert(app.TaskCount, check.Equals, 5)
	c.Assert(app.VersionCount, check.Equals, 1)
	c.Assert(len(app.Version), check.Equals, 1)

	// do canary update

	body := &types.CanaryUpdateBody{
		Version:   demoVersion().setName("demo").setCount(5).setCPU(0.01).setMem(10).Get(),
		Instances: 3,
		Value:     0.5,
		OnFailure: "continue",
		Delay:     0.5,
	}
	s.canaryUpdate(id, body, c)
	err = s.waitApp(id, types.OpStatusCanaryUnfinished, time.Second*180, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() updated")

	// verify app
	app = s.inspectApp(id, c)
	c.Assert(app.Name, check.Equals, "demo")
	c.Assert(app.VersionCount, check.Equals, 2)
	c.Assert(len(app.Version), check.Equals, 2)
	c.Assert(app.OpStatus, check.Equals, types.OpStatusCanaryUnfinished)

	// verify app tasks
	tasks := s.listAppTasks(id, c)
	c.Assert(len(tasks), check.Equals, 5)

	var n, m int
	for _, task := range tasks {
		if task.Weight == 67 {
			n++
		}

		if task.Weight == 100 {
			m++
		}
	}

	c.Assert(n, check.Equals, 3)
	c.Assert(m, check.Equals, 2)

	// verify app versions
	vers := s.listAppVersions(id, c)
	c.Assert(len(vers), check.Equals, 2)
	c.Assert(vers[0].Mem, check.Equals, float64(10))

	counter := make(map[string]int)
	for _, task := range tasks {
		if v, ok := counter[task.Version]; ok {
			v++
			counter[task.Version] = v
		} else {
			counter[task.Version] = 1
		}
	}

	c.Assert(counter[vers[0].ID], check.Equals, 3)
	c.Assert(counter[vers[1].ID], check.Equals, 2)

	// canary continue
	body = &types.CanaryUpdateBody{
		Instances: 5,
		Value:     0.5,
		OnFailure: "continue",
		Delay:     0.5,
	}
	s.canaryUpdate(id, body, c)
	err = s.waitApp(id, types.OpStatusNoop, time.Second*180, c)
	c.Assert(err, check.IsNil)
	fmt.Println("TestCanaryUpdate() continued")

	// verify app
	app = s.inspectApp(id, c)
	c.Assert(app.Name, check.Equals, "demo")
	c.Assert(app.VersionCount, check.Equals, 2)
	c.Assert(len(app.Version), check.Equals, 1)
	c.Assert(app.OpStatus, check.Equals, types.OpStatusNoop)

	// verify app tasks
	tasks = s.listAppTasks(id, c)
	c.Assert(len(tasks), check.Equals, 5)

	var y int
	for _, task := range tasks {
		if task.Weight == 100 {
			y++
		}
	}

	c.Assert(y, check.Equals, 5)

	// verify app versions
	vers = s.listAppVersions(id, c)
	c.Assert(len(vers), check.Equals, 2)
	c.Assert(vers[0].Mem, check.Equals, float64(10))

	counter = make(map[string]int)
	for _, task := range tasks {
		if v, ok := counter[task.Version]; ok {
			v++
			counter[task.Version] = v
		} else {
			counter[task.Version] = 1
		}
	}

	c.Assert(counter[vers[0].ID], check.Equals, 5)

	// clean up

	err = s.removeApp(id, time.Second*10, c)
	c.Assert(err, check.IsNil)
{'fraction_non_alphanumeric': 0.17642818525860515, 'fraction_numerical': 0.0223463687150838, 'mean_word_length': 3.796888504753673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965405', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2231, 'n_words': 444}