From b715e6e84b8ccc2f5367a5e360598f3ffaa3517d Mon Sep 17 00:00:00 2001 From: Pierce Lopez Date: Sun, 8 Jan 2017 18:59:42 -0500 Subject: [PATCH] tests for nsqd metadata migration to new filename --- nsqd/nsqd_test.go | 109 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/nsqd/nsqd_test.go b/nsqd/nsqd_test.go index 51e61dacb..ab1fe2095 100644 --- a/nsqd/nsqd_test.go +++ b/nsqd/nsqd_test.go @@ -23,8 +23,15 @@ const ( RequestTimeout = 5 * time.Second ) +func newMetadataFile(opts *Options) string { + return path.Join(opts.DataPath, "nsqd.dat") +} +func oldMetadataFile(opts *Options) string { + return path.Join(opts.DataPath, fmt.Sprintf("nsqd.%d.dat", opts.ID)) +} + func getMetadata(n *NSQD) (*meta, error) { - fn := path.Join(n.getOpts().DataPath, "nsqd.dat") + fn := newMetadataFile(n.getOpts()) data, err := ioutil.ReadFile(fn) if err != nil { return nil, err @@ -158,6 +165,106 @@ func TestStartup(t *testing.T) { <-doneExitChan } +func TestMetadataMigrate(t *testing.T) { + old_meta := ` + { + "topics": [ + { + "channels": [ + {"name": "c1", "paused": false}, + {"name": "c2", "paused": false} + ], + "name": "t1", + "paused": false + } + ], + "version": "1.0.0-alpha" + }` + + tmpDir, err := ioutil.TempDir("", "nsq-test-") + if err != nil { + panic(err) + } + defer os.RemoveAll(tmpDir) + + opts := NewOptions() + opts.DataPath = tmpDir + opts.Logger = test.NewTestLogger(t) + + oldFn := oldMetadataFile(opts) + err = ioutil.WriteFile(oldFn, []byte(old_meta), 0700) + if err != nil { + panic(err) + } + + _, _, nsqd := mustStartNSQD(opts) + err = nsqd.LoadMetadata() + test.Nil(t, err) + err = nsqd.PersistMetadata() + test.Nil(t, err) + nsqd.Exit() + + oldFi, err := os.Lstat(oldFn) + test.Nil(t, err) + test.Equal(t, oldFi.Mode()&os.ModeType, os.ModeSymlink) + + _, _, nsqd = mustStartNSQD(opts) + err = nsqd.LoadMetadata() + test.Nil(t, err) + + t1, err := nsqd.GetExistingTopic("t1") + test.Nil(t, err) + test.NotNil(t, t1) + c2, err := t1.GetExistingChannel("c2") + test.Nil(t, err) + test.NotNil(t, c2) + + nsqd.Exit() +} + +func TestMetadataConflict(t *testing.T) { + old_meta := ` + { + "topics": [{ + "name": "t1", "paused": false, + "channels": [{"name": "c1", "paused": false}] + }], + "version": "1.0.0-alpha" + }` + new_meta := ` + { + "topics": [{ + "name": "t2", "paused": false, + "channels": [{"name": "c2", "paused": false}] + }], + "version": "1.0.0-alpha" + }` + + tmpDir, err := ioutil.TempDir("", "nsq-test-") + if err != nil { + panic(err) + } + defer os.RemoveAll(tmpDir) + + opts := NewOptions() + opts.DataPath = tmpDir + opts.Logger = test.NewTestLogger(t) + + err = ioutil.WriteFile(oldMetadataFile(opts), []byte(old_meta), 0700) + if err != nil { + panic(err) + } + err = ioutil.WriteFile(newMetadataFile(opts), []byte(new_meta), 0700) + if err != nil { + panic(err) + } + + _, _, nsqd := mustStartNSQD(opts) + err = nsqd.LoadMetadata() + test.NotNil(t, err) + nsqd.Exit() +} + func TestEphemeralTopicsAndChannels(t *testing.T) { // ephemeral topics/channels are lazily removed after the last channel/client is removed opts := NewOptions()