Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
tests round 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Gromov committed Aug 17, 2017
1 parent ac43189 commit 6963a2d
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 218 deletions.
33 changes: 0 additions & 33 deletions rules/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package rules

import (
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -90,22 +89,6 @@ func newMappingRuleSnapshotJSON(mrs mappingRuleSnapshot) mappingRuleSnapshotJSON
}
}

// MarshalJSON returns the JSON encoding of mappingRuleSnapshots
func (mrs mappingRuleSnapshot) MarshalJSON() ([]byte, error) {
return json.Marshal(newMappingRuleSnapshotJSON(mrs))
}

// UnmarshalJSON unmarshals JSON-encoded data into mappingRuleSnapshots
func (mrs *mappingRuleSnapshot) UnmarshalJSON(data []byte) error {
var mrsj mappingRuleSnapshotJSON
err := json.Unmarshal(data, &mrsj)
if err != nil {
return err
}
*mrs = mrsj.mappingRuleSnapshot()
return nil
}

func (mrsj mappingRuleSnapshotJSON) mappingRuleSnapshot() mappingRuleSnapshot {
return mappingRuleSnapshot{
name: mrsj.Name,
Expand Down Expand Up @@ -303,22 +286,6 @@ func newMappingRuleJSON(mc mappingRule) mappingRuleJSON {
}
}

// MarshalJSON returns the JSON encoding of mappingRuleSnapshots
func (mc mappingRule) MarshalJSON() ([]byte, error) {
return json.Marshal(newMappingRuleJSON(mc))
}

// UnmarshalJSON unmarshals JSON-encoded data into mappingRuleSnapshots
func (mc *mappingRule) UnmarshalJSON(data []byte) error {
var mrj mappingRuleJSON
err := json.Unmarshal(data, &mrj)
if err != nil {
return err
}
*mc = mrj.mappingRule()
return nil
}

func (mrj mappingRuleJSON) mappingRule() mappingRule {
snapshots := make([]*mappingRuleSnapshot, len(mrj.Snapshots))
for i, s := range mrj.Snapshots {
Expand Down
47 changes: 0 additions & 47 deletions rules/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package rules

import (
"bytes"
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -203,51 +201,6 @@ func TestMappingRuleSchema(t *testing.T) {
require.Equal(t, testMappingRuleSchema, schema)
}

func TestMarshalMappingRule(t *testing.T) {
marshalledRule := `{
"uuid":"12669817-13ae-40e6-ba2f-33087b262c68",
"snapshots":[
{"name":"foo",
"tombstoned":false,
"cutoverNanos":12345,
"filters":{"tag1":"value1","tag2":"value2"},
"policies":["10s@1s:24h0m0s|P999"]
},
{"name":"bar",
"tombstoned":true,
"cutoverNanos":67890,
"filters":{"tag3":"value3","tag4":"value4"},
"policies":["1m0s@1m:24h0m0s","5m0s@1m:48h0m0s"]}
]
}`
mr, err := newMappingRule(testMappingRuleSchema, testTagsFilterOptions())
res, err := mr.MarshalJSON()
require.NoError(t, err)

var bb bytes.Buffer
err = json.Compact(&bb, []byte(marshalledRule))
require.NoError(t, err)

require.Equal(t, bb.String(), string(res))
}

func TestUnmarshalMappingRule(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, testTagsFilterOptions())
data, err := mr.MarshalJSON()

var mr2 mappingRule
err = json.Unmarshal(data, &mr2)
require.NoError(t, err)

expected, err := mr.Schema()
require.NoError(t, err)

actual, err := mr2.Schema()
require.NoError(t, err)

require.Equal(t, expected, actual)
}

func TestNewMappingRuleFromFields(t *testing.T) {
filterOpts := testTagsFilterOptions()
rawFilters := map[string]string{"tag3": "value3"}
Expand Down
142 changes: 142 additions & 0 deletions rules/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,145 @@ func TestNamespaceTombstoneAlreadyDead(t *testing.T) {
err = ns.markTombstoned(4)
require.Error(t, err)
}

func TestNamespaceAdd(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: true},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)
nssClone, err := nss.Clone()
require.NoError(t, err)

err = nssClone.AddNamespace("bar")
require.NoError(t, err)

ns, err := nssClone.Namespace("bar")
require.NoError(t, err)
require.False(t, ns.Tombstoned())

ns, err = nss.Namespace("bar")
require.Error(t, err)
}

func TestNamespaceAddDup(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: false},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)

err = nss.AddNamespace("foo")
require.Error(t, err)
}

func TestNamespaceRevive(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: false},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)
ns, err := nss.Namespace("foo")
require.NoError(t, err)

err = nss.DeleteNamespace("foo", 4)
require.NoError(t, err)

ns, err = nss.Namespace("foo")
require.True(t, ns.Tombstoned())

err = nss.AddNamespace("foo")
require.NoError(t, err)

ns, err = nss.Namespace("foo")
require.NoError(t, err)
require.Equal(t, ns.snapshots[len(ns.snapshots)-1].forRuleSetVersion, 5)
require.Equal(t, len(ns.snapshots), 3)
}

func TestNamespaceDelete(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: false},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)
ns, err := nss.Namespace("foo")
require.NoError(t, err)

err = nss.DeleteNamespace("foo", 4)
require.NoError(t, err)
ns, err = nss.Namespace("foo")
require.NoError(t, err)
require.True(t, ns.Tombstoned())
require.Equal(t, ns.snapshots[len(ns.snapshots)-1].forRuleSetVersion, 5)
}

func TestNamespaceDeleteMissing(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: false},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)

err = nss.DeleteNamespace("bar", 4)
require.Error(t, err)
}

func TestNamespaceDeleteTombstoned(t *testing.T) {
testNss := &schema.Namespaces{
Namespaces: []*schema.Namespace{
&schema.Namespace{
Name: "foo",
Snapshots: []*schema.NamespaceSnapshot{
&schema.NamespaceSnapshot{ForRulesetVersion: 1, Tombstoned: true},
},
},
},
}

nss, err := NewNamespaces(1, testNss)
require.NoError(t, err)

err = nss.DeleteNamespace("foo", 4)
require.Error(t, err)
}
56 changes: 0 additions & 56 deletions rules/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package rules

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sort"
Expand Down Expand Up @@ -83,22 +82,6 @@ func newRollupTargetJSON(rt RollupTarget) rollupTargetJSON {
return rollupTargetJSON{Name: string(rt.Name), Tags: stringArrayFromBytesArray(rt.Tags), Policies: rt.Policies}
}

// MarshalJSON ...
func (t RollupTarget) MarshalJSON() ([]byte, error) {
return json.Marshal(newRollupTargetJSON(t))
}

// UnmarshalJSON unmarshals JSON-encoded data into staged policies.
func (t *RollupTarget) UnmarshalJSON(data []byte) error {
var tj rollupTargetJSON
err := json.Unmarshal(data, &tj)
if err != nil {
return err
}
*t = tj.rollupTarget()
return nil
}

func (tj rollupTargetJSON) rollupTarget() RollupTarget {
return NewRollupTargetFromFields(tj.Name, tj.Tags, tj.Policies)
}
Expand Down Expand Up @@ -155,22 +138,6 @@ func newRollupRuleSnapshotJSON(rrs rollupRuleSnapshot) rollupRuleSnapshotJSON {
}
}

// MarshalJSON returns the JSON encoding of rollupRuleSnapshots
func (rrs rollupRuleSnapshot) MarshalJSON() ([]byte, error) {
return json.Marshal(newRollupRuleSnapshotJSON(rrs))
}

// UnmarshalJSON unmarshals JSON-encoded data into rollupRuleSnapshots
func (rrs *rollupRuleSnapshot) UnmarshalJSON(data []byte) error {
var rrsj rollupRuleSnapshotJSON
err := json.Unmarshal(data, &rrsj)
if err != nil {
return err
}
*rrs = rrsj.rollupRuleSnapshot()
return nil
}

func (rrsj rollupRuleSnapshotJSON) rollupRuleSnapshot() rollupRuleSnapshot {
targets := make([]RollupTarget, len(rrsj.Targets))
for i, t := range rrsj.Targets {
Expand Down Expand Up @@ -354,13 +321,6 @@ func (rc *rollupRule) Tombstoned() bool {
return latest.tombstoned
}

func (rc rollupRule) targets() []RollupTarget {
if len(rc.snapshots) == 0 {
return nil
}
return rc.snapshots[len(rc.snapshots)-1].targets
}

func (rc *rollupRule) addSnapshot(
name string,
rawFilters map[string]string,
Expand Down Expand Up @@ -443,22 +403,6 @@ func newRollupRuleJSON(rc rollupRule) rollupRuleJSON {
}
}

// MarshalJSON returns the JSON encoding of mappingRuleSnapshots
func (rc rollupRule) MarshalJSON() ([]byte, error) {
return json.Marshal(newRollupRuleJSON(rc))
}

// UnmarshalJSON unmarshals JSON-encoded data into mappingRuleSnapshots
func (rc *rollupRule) UnmarshalJSON(data []byte) error {
var rrj rollupRuleJSON
err := json.Unmarshal(data, &rrj)
if err != nil {
return err
}
*rc = rrj.rollupRule()
return nil
}

func (rrj rollupRuleJSON) rollupRule() rollupRule {
snapshots := make([]*rollupRuleSnapshot, len(rrj.Snapshots))
for i, s := range rrj.Snapshots {
Expand Down
Loading

0 comments on commit 6963a2d

Please sign in to comment.