示例#1
0
func TestUnmarshal(t *testing.T) {
	w := testutils.Wrap(t)

	var err error
	version := semver.MustParse("0.0.1")

	jsonData := []byte(`{
        "version": "0.0.1",
        "path": "./test_artifacts"
    }`)

	cfg := config.NewConfig(version)

	err = json.Unmarshal(jsonData, &cfg)
	w.BailIfErrorf(err, "deserializazion failed: %v", err)

	provider, err := FromConfig(cfg)
	w.BailIfErrorf(err, "instantiation failed: %v", err)

	root, err := provider.Root()
	w.BailIfError(err)

	_, err = checkDirectoryContents(root, expectedRootEntries())

	w.BailIfErrorf(err, "provider path differs")
}
示例#2
0
func TestMarshalUnmarshal(t *testing.T) {
	w := testutils.Wrap(t)

	var err error
	version := semver.MustParse("0.0.1")

	provider := getFSInstace()
	cfg := provider.Marshal()

	jsonData, err := json.Marshal(cfg)
	w.BailIfErrorf(err, "serialization failed: %v", err)

	unmarshalCfg := config.NewConfig(version)

	err = json.Unmarshal(jsonData, &unmarshalCfg)
	w.BailIfErrorf(err, "deserialization failed: %v", err)

	unmarshalledProvider, err := FromConfig(unmarshalCfg)
	w.BailIfErrorf(err, "instatiation failed: %v", err)

	root, err := unmarshalledProvider.Root()
	w.BailIfError(err)

	_, err = checkDirectoryContents(root, expectedRootEntries())

	w.BailIfErrorf(err, "provider path differs")
}
示例#3
0
func TestMarshalUnmarshal(t *testing.T) {
	w := testutils.Wrap(t)

	var err error
	version := semver.MustParse("0.0.1")

	provider, err := local.NewLocalFS("./test_artifacts")

	w.BailIfError(err)

	cfg := provider.Marshal()
	jsonData, err := json.Marshal(cfg)

	w.BailIfErrorf(err, "serialization failed: %v", err)

	if !strings.Contains(string(jsonData), "test_artifacts") {
		t.Fatalf("JSON does not contain the proper path: %s", string(jsonData))
	}

	unmarshalledConfig := config.NewConfig(version)
	err = json.Unmarshal(jsonData, &unmarshalledConfig)

	w.BailIfErrorf(err, "deserialization failed: %v", err)

	_, err = FromConfig(unmarshalledConfig)

	w.BailIfErrorf(err, "reinstation of fs provider failed: %v", err)
}
示例#4
0
func TestBasicUsage(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	repo, err := newLocalFSRepository(".")
	w.BailIfError(err)

	registry := NewRegistry()

	if registry.Size() != 0 {
		t.Fatalf("registry should start empty, got size: %d", registry.Size())
	}

	if registry.Get("foo") != nil {
		t.Fatalf("Getting a nonexisting key should return nil, got %v", registry.Get("foo"))
	}

	registry.Set("foo", repo)

	if registry.Size() != 1 {
		t.Fatalf("registry should grow to size 1 after set, got %d", registry.Size())
	}

	if registry.Get("foo") != repo {
		t.Fatalf("failed to retrieve repo")
	}

	registry.Unset("foo")
	if registry.Size() != 0 {
		t.Fatalf("clearing the key failed")
	}
}
示例#5
0
func TestUnmarshallEmpty(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	_, err = unmarshalConfig([]byte(`{}`))

	w.ShouldFailf(err, "unmarshalling should fail if version is missing")
}
示例#6
0
func TestMarshalWithLocalFS(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	cwd, err := os.Getwd()
	w.BailIfError(err)

	referenceJson := []byte(fmt.Sprintf(`{
	    "foo": {
            "plain": true,
            "storage": {
                "type": "local",
                "path": "%s"
            }
        },
        "bar": {
            "plain": true,
            "storage": {
                "type": "local",
                "path": "%s"
            }
        }
	}`, cwd, cwd))

	registry := NewRegistry()

	storage1, err := local.NewLocalFS(".")
	w.BailIfErrorf(err, "unable to init storage: %v", err)

	storage2, err := local.NewLocalFS(".")
	w.BailIfErrorf(err, "unable to init storage: %v", err)

	repo1 := repository.NewRepository(true, storage1)
	repo2 := repository.NewRepository(true, storage2)

	registry.Set("foo", repo1)
	registry.Set("bar", repo2)

	cfg := registry.Marshal()
	jsonData, err := json.Marshal(cfg)
	w.BailIfErrorf(err, "serialization failed: %v", err)

	match, err := testutils.JsonEqual(jsonData, referenceJson)
	w.BailIfErrorf(err, "error parsing JSON: %v", err)

	if !match {
		t.Fatalf("JSON did not match:\n%s\nvs.\n%s", string(jsonData), string(referenceJson))
	}
}
示例#7
0
func TestUnmarshalLocalWithInvalidJSON(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	version := semver.MustParse("0.0.1")
	jsonData := `{
	           "type": "local",
	           "path": "."
	       `

	cfg := config.NewConfig(version)
	err = json.Unmarshal([]byte(jsonData), &cfg)

	w.ShouldFailf(err, "unmarshalling should fail due to invalid JSON")
}
示例#8
0
func TestUnmarshalWithLocalFS(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	version := semver.MustParse("0.0.1")

	jsonData := []byte(`{
        "foo": {
            "plain": true,
            "storage": {
                "type": "local",
                "path": "."
            }
        },
        "bar": {
            "plain": true,
            "storage": {
                "type": "local",
                "path": ".."
            }
        }
    }`)

	cfg := config.NewConfig(version)

	err = json.Unmarshal(jsonData, &cfg)
	w.BailIfErrorf(err, "deserialization failed: %v", err)

	registry, err := CreateFromConfig(cfg)
	w.BailIfErrorf(err, "unmarshalling failed: %v", err)

	if registry.Size() != 2 {
		t.Fatalf("registry has the wrong size; expected 2, got %d", registry.Size())
	}

	if registry.Get("foo") == nil {
		t.Fatalf("foo: no such key")
	}

	if registry.Get("bar") == nil {
		t.Fatalf("bar: no such key")
	}
}
示例#9
0
func TestGetKeys(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	repo1, err := newLocalFSRepository(".")
	w.BailIfError(err)

	repo2, err := newLocalFSRepository("..")
	w.BailIfError(err)

	registry := NewRegistry()

	registry.Set("foo", repo1)
	registry.Set("bar", repo2)

	keys := registry.Keys()
	sort.Strings(keys)

	if !reflect.DeepEqual(keys, []string{"bar", "foo"}) {
		t.Fatalf("keys do not match: %v", keys)
	}

	all := registry.All()

	if len(all) != 2 {
		t.Fatalf("len mismatch: %d vs. 2", len(all))
	}

	var (
		exist bool
		repo  repository.Repository
	)

	repo, exist = all["foo"]
	if !exist || repo != repo1 {
		t.Fatal("unable to retrieve foo")
	}

	repo, exist = all["bar"]
	if !exist || repo != repo2 {
		t.Fatalf("unable to retrieve bar")
	}
}
示例#10
0
func TestMarshall(t *testing.T) {
	w := testutils.Wrap(t)
	version := semver.MustParse("1.2.3")

	var err error

	config := NewMutableConfig(version)
	jsonData, err := marshalConfig(config)

	w.BailIfError(err)

	unmarshalledConfig, err := unmarshalConfig(jsonData)

	w.BailIfError(err)

	if actualVersion := unmarshalledConfig.CsyncVersion(); actualVersion.NE(version) {
		t.Fatalf("unmarshalled wrong version: expected %s, got %s", version, actualVersion)
	}
}
示例#11
0
func TestUnmarshalLocalWithInvalidPath(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	version := semver.MustParse("0.0.1")
	jsonData := `{
	           "type": "local",
	           "path": "./INVALID_PATH"
	       }`

	cfg := config.NewConfig(version)
	err = json.Unmarshal([]byte(jsonData), &cfg)

	w.BailIfErrorf(err, "unmarshalling failed: %v", err)

	_, err = FromConfig(cfg)

	w.ShouldFailf(err, "unmarshalling should fail due to the invalid path")
}
示例#12
0
func TestUnmarshalLocal(t *testing.T) {
	w := testutils.Wrap(t)

	var err error

	version := semver.MustParse("0.0.1")
	jsonData := `{
	           "type": "local",
	           "path": "."
	       }`

	cfg := config.NewConfig(version)
	err = json.Unmarshal([]byte(jsonData), &cfg)

	w.BailIfErrorf(err, "unmarshalling failed: %v", err)

	_, err = FromConfig(cfg)

	w.BailIfErrorf(err, "creating the provider failed: %v", err)
}
示例#13
0
func TestSave(t *testing.T) {
	w := testutils.Wrap(t)
	version := semver.MustParse("1.2.3")

	var err error

	locator, err := newMockLocator()
	w.BailIfError(err)
	defer locator.Destroy()

	manager := NewManager(locator)
	err = manager.Save(NewMutableConfig(version))

	w.BailIfErrorf(err, "saving the config failed: %v", err)

	config, err := manager.Load()

	w.BailIfErrorf(err, "loading the config failed: %v", err)

	if actualVersion := config.CsyncVersion(); actualVersion.NE(version) {
		t.Fatalf("loaded wrong version: expected %s, got %s", version, actualVersion)
	}
}