Exemple #1
0
func testRollbackCommandURL(t *testing.T, u *url.URL) {
	ctx := testContext(t, u)

	// drop, recreate, and migrate database
	err := DropCommand(ctx)
	require.Nil(t, err)
	err = CreateCommand(ctx)
	require.Nil(t, err)
	err = MigrateCommand(ctx)
	require.Nil(t, err)

	// verify migration
	db, err := GetDriverOpen(u)
	require.Nil(t, err)
	defer mustClose(db)

	count := 0
	err = db.QueryRow(`select count(*) from schema_migrations
		where version = '20151129054053'`).Scan(&count)
	require.Nil(t, err)
	require.Equal(t, 1, count)

	// rollback
	err = RollbackCommand(ctx)
	require.Nil(t, err)

	// verify rollback
	err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
	require.Nil(t, err)
	require.Equal(t, 0, count)

	err = db.QueryRow("select count(*) from users").Scan(&count)
	require.NotNil(t, err)
	require.Regexp(t, "(does not exist|doesn't exist|no such table)", err.Error())
}
Exemple #2
0
func TestMySQLDatabaseExists_Error(t *testing.T) {
	drv := MySQLDriver{}
	u := mySQLTestURL(t)
	u.User = url.User("invalid")

	exists, err := drv.DatabaseExists(u)
	require.Regexp(t, "Access denied for user 'invalid'@", err.Error())
	require.Equal(t, false, exists)
}
Exemple #3
0
func TestMySQLCreateMigrationsTable(t *testing.T) {
	drv := MySQLDriver{}
	db := prepTestMySQLDB(t)
	defer mustClose(db)

	// migrations table should not exist
	count := 0
	err := db.QueryRow("select count(*) from schema_migrations").Scan(&count)
	require.Regexp(t, "Table 'dbmate.schema_migrations' doesn't exist", err.Error())

	// create table
	err = drv.CreateMigrationsTable(db)
	require.Nil(t, err)

	// migrations table should exist
	err = db.QueryRow("select count(*) from schema_migrations").Scan(&count)
	require.Nil(t, err)

	// create table should be idempotent
	err = drv.CreateMigrationsTable(db)
	require.Nil(t, err)
}
Exemple #4
0
func TestMySQLCreateDropDatabase(t *testing.T) {
	drv := MySQLDriver{}
	u := mySQLTestURL(t)

	// drop any existing database
	err := drv.DropDatabase(u)
	require.Nil(t, err)

	// create database
	err = drv.CreateDatabase(u)
	require.Nil(t, err)

	// check that database exists and we can connect to it
	func() {
		db, err := drv.Open(u)
		require.Nil(t, err)
		defer mustClose(db)

		err = db.Ping()
		require.Nil(t, err)
	}()

	// drop the database
	err = drv.DropDatabase(u)
	require.Nil(t, err)

	// check that database no longer exists
	func() {
		db, err := drv.Open(u)
		require.Nil(t, err)
		defer mustClose(db)

		err = db.Ping()
		require.NotNil(t, err)
		require.Regexp(t, "Unknown database 'dbmate'", err.Error())
	}()
}
Exemple #5
0
// Test that forward.go plugs everything together correctly, and
// exercise the tcp shim.
func TestForward(t *testing.T) {
	nc := netConfig{
		chain:  "FLUX",
		bridge: "lo",
	}

	mipt := newMockIPTables(t)
	ipTables := newIPTables(nc, mipt.cmd)
	ipTables.start()

	listener, err := net.ListenTCP("tcp", nil)
	require.Nil(t, err)
	laddr := listener.Addr().(*net.TCPAddr)

	errorSink := daemon.NewErrorSink()
	ss, err := forwardingConfig{
		netConfig:    nc,
		ipTables:     ipTables,
		eventHandler: events.NullHandler{},
		errorSink:    errorSink,
	}.start(&model.Service{
		Name:     "service",
		Protocol: "tcp",
		IP:       net.ParseIP("127.42.0.1"),
		Port:     8888,
		Instances: []model.Instance{
			{
				Name:  "inst",
				Group: "group",
				IP:    laddr.IP,
				Port:  laddr.Port,
			},
		},
	})
	require.Nil(t, err)

	require.Len(t, mipt.chains["nat FLUX"], 1)
	rule := mipt.chains["nat FLUX"][0]
	require.Regexp(t, "^-p tcp -d 127\\.42\\.0\\.1 --dport 8888 -j DNAT --to-destination 127\\.0\\.0\\.1:\\d+$", strings.Join(rule, " "))

	rng := rand.New(rand.NewSource(time.Now().UnixNano()))
	expect := fmt.Sprint(rng.Int63())
	got := ""

	go func() {
		for {
			conn, err := listener.AcceptTCP()
			if err != nil {
				return
			}

			b, err := ioutil.ReadAll(conn)
			require.Nil(t, err)
			require.Nil(t, conn.Close())
			got = string(b)
		}
	}()

	faddr, err := net.ResolveTCPAddr("tcp", rule[len(rule)-1])
	require.Nil(t, err)
	conn, err := net.DialTCP("tcp", nil, faddr)
	require.Nil(t, err)
	_, err = conn.Write([]byte(expect))
	require.Nil(t, err)
	require.Nil(t, conn.CloseWrite())
	_, err = ioutil.ReadAll(conn)
	require.Nil(t, err)
	require.Nil(t, conn.Close())
	require.Equal(t, expect, got)

	listener.Close()
	ss.stop()
}
func TestFileChangeIterator(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Fatal(err.Error())
	}
	defer os.RemoveAll(tmpDir)

	cl, err := NewFileChangelist(tmpDir)
	require.Nil(t, err, "Error initializing fileChangelist")

	it, err := cl.NewIterator()
	require.Nil(t, err, "Error initializing iterator")
	require.False(t, it.HasNext(), "HasNext returns false for empty ChangeList")

	c1 := NewTUFChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1})
	cl.Add(c1)

	c2 := NewTUFChange(ActionUpdate, "t2", "target2", "test/targ2", []byte{2})
	cl.Add(c2)

	c3 := NewTUFChange(ActionUpdate, "t3", "target3", "test/targ3", []byte{3})
	cl.Add(c3)

	cs := cl.List()
	index := 0
	it, err = cl.NewIterator()
	require.Nil(t, err, "Error initializing iterator")
	for it.HasNext() {
		c, err := it.Next()
		require.Nil(t, err, "Next err should be false")
		require.Equal(t, c.Action(), cs[index].Action(), "Action mismatch")
		require.Equal(t, c.Scope(), cs[index].Scope(), "Scope mismatch")
		require.Equal(t, c.Type(), cs[index].Type(), "Type mismatch")
		require.Equal(t, c.Path(), cs[index].Path(), "Path mismatch")
		require.Equal(t, c.Content(), cs[index].Content(), "Content mismatch")
		index++
	}
	require.Equal(t, index, len(cs), "Iterator produced all data in ChangeList")

	// negative test case: index out of range
	_, err = it.Next()
	require.Error(t, err, "Next errors gracefully when exhausted")
	var iterError IteratorBoundsError
	require.IsType(t, iterError, err, "IteratorBoundsError type")
	require.Regexp(t, "out of bounds", err, "Message for iterator bounds error")

	// negative test case: changelist files missing
	it, err = cl.NewIterator()
	require.Nil(t, err, "Error initializing iterator")
	for it.HasNext() {
		cl.Clear("")
		_, err := it.Next()
		require.Error(t, err, "Next() error for missing changelist files")
	}

	// negative test case: bad JSON file to unmarshall via Next()
	cl.Clear("")
	ioutil.WriteFile(filepath.Join(tmpDir, "broken_file.change"), []byte{5}, 0644)
	it, err = cl.NewIterator()
	require.Nil(t, err, "Error initializing iterator")
	for it.HasNext() {
		_, err := it.Next()
		require.Error(t, err, "Next should indicate error for bad JSON file")
	}

	// negative test case: changelist directory does not exist
	os.RemoveAll(tmpDir)
	it, err = cl.NewIterator()
	require.Error(t, err, "Initializing iterator without underlying file store")
}
Exemple #7
0
func requireForwarding(t *testing.T, mipt *mockIPTables) {
	require.Len(t, mipt.chains["nat FLUX"], 1)
	require.Len(t, mipt.chains["filter FLUX"], 0)
	// NB regexp related to service IP and port given in test case
	require.Regexp(t, "^-p tcp -d 127\\.42\\.0\\.1 --dport 8888 -j DNAT --to-destination 127\\.0\\.0\\.1:\\d+$", strings.Join(mipt.chains["nat FLUX"][0], " "))
}
func assertLogFormatAndCompareContent(t *testing.T, logline, expected string) {
	rx := regexp.MustCompile(`\.*({.*})`)
	require.Regexp(t, rx, logline)
	actual := rx.FindStringSubmatch(logline)[1]
	compareJSONStrings(t, expected, actual)
}