func TestValidate_NoDataDir(t *testing.T) { bagValidationConfig, err := getValidationConfig() if err != nil { assert.Fail(t, "Could not load BagValidationConfig: %s", err.Error()) } optionalFileSpec := validation.FileSpec{Presence: "OPTIONAL"} bagValidationConfig.FileSpecs["tagmanifest-md5.txt"] = optionalFileSpec _, filename, _, _ := runtime.Caller(0) dir := filepath.Dir(filename) pathToBag, err := filepath.Abs(path.Join(dir, "..", "testdata", "unit_test_bags", "example.edu.sample_no_data_dir.tar")) validator, err := validation.NewBagValidator(pathToBag, bagValidationConfig) if err != nil { assert.Fail(t, "NewBagValidator returned unexpected error: %s", err.Error()) } result := validator.Validate() assert.NotNil(t, result.IntellectualObject) assert.Equal(t, 5, len(result.IntellectualObject.GenericFiles)) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "File 'data/datastream-DC' in manifest 'manifest-md5.txt' is missing")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "File 'data/datastream-descMetadata' in manifest 'manifest-md5.txt' is missing")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "File 'data/datastream-MARC' in manifest 'manifest-md5.txt' is missing")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "File 'data/datastream-RELS-EXT' in manifest 'manifest-md5.txt' is missing")) assert.True(t, result.ParseSummary.HasErrors()) assert.NotNil(t, result.ValidationSummary) require.True(t, result.ValidationSummary.HasErrors()) }
func TestNewBagValidator_BadConfig(t *testing.T) { _, filename, _, _ := runtime.Caller(0) dir := filepath.Dir(filename) pathToBag, err := filepath.Abs(path.Join(dir, "..", "testdata", "unit_test_bags", "example.edu.tagsample_good.tar")) if err != nil { assert.Fail(t, "Can't figure out Abs path: %s", err.Error()) } bagValidationConfig, err := getValidationConfig() if err != nil { assert.Fail(t, "Could not load BagValidationConfig: %v", err) } badPathSpec := validation.TagSpec{ FilePath: "", Presence: "REQUIRED", EmptyOK: true, } badPresenceSpec := validation.TagSpec{ FilePath: "orangina", Presence: "orangina", EmptyOK: true, } bagValidationConfig.TagSpecs["bad_path_spec"] = badPathSpec bagValidationConfig.TagSpecs["bad_presence"] = badPresenceSpec _, err = validation.NewBagValidator(pathToBag, bagValidationConfig) require.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "TagSpec for file ''")) assert.True(t, strings.Contains(err.Error(), "TagSpec for file 'orangina'")) }
func TestValidate_BadFileNames(t *testing.T) { bagValidationConfig, err := getValidationConfig() if err != nil { assert.Fail(t, "Could not load BagValidationConfig: %s", err.Error()) } optionalFileSpec := validation.FileSpec{Presence: "OPTIONAL"} bagValidationConfig.FileSpecs["tagmanifest-md5.txt"] = optionalFileSpec _, filename, _, _ := runtime.Caller(0) dir := filepath.Dir(filename) pathToBag, err := filepath.Abs(path.Join(dir, "..", "testdata", "unit_test_bags", "example.edu.sample_bad_file_names.tar")) validator, err := validation.NewBagValidator(pathToBag, bagValidationConfig) if err != nil { assert.Fail(t, "NewBagValidator returned unexpected error: %s", err.Error()) } result := validator.Validate() assert.NotNil(t, result.IntellectualObject) assert.Equal(t, 9, len(result.IntellectualObject.GenericFiles)) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "Filename 'data/-starts-with-dash'")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "Filename 'data/contains#hash'")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "Filename 'data/contains*star'")) assert.True(t, strings.Contains(result.IntellectualObject.IngestErrorMessage, "Filename 'data/contains+plus'")) assert.False(t, result.ParseSummary.HasErrors()) assert.NotNil(t, result.ValidationSummary) require.True(t, result.ValidationSummary.HasErrors()) }
func (a *Accumulator) AssertContainsTaggedFields( t *testing.T, measurement string, fields map[string]interface{}, tags map[string]string, ) { for _, p := range a.Points { if !reflect.DeepEqual(tags, p.Tags) { continue } if p.Measurement == measurement { if !reflect.DeepEqual(fields, p.Fields) { pActual, _ := json.MarshalIndent(p.Fields, "", " ") pExp, _ := json.MarshalIndent(fields, "", " ") msg := fmt.Sprintf("Actual:\n%s\n(%T) \nExpected:\n%s\n(%T)", string(pActual), p.Fields, string(pExp), fields) assert.Fail(t, msg) } return } } msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags) assert.Fail(t, msg) }
func TestLoadBagValidationConfig(t *testing.T) { configFilePath := path.Join("testdata", "json_objects", "bag_validation_config.json") conf, errors := validation.LoadBagValidationConfig(configFilePath) if errors != nil && len(errors) > 0 { assert.Fail(t, errors[0].Error()) } assert.True(t, conf.AllowMiscTopLevelFiles) assert.True(t, conf.AllowMiscDirectories) assert.True(t, conf.TopLevelDirMustMatchBagName) assert.Equal(t, 7, len(conf.FileSpecs)) assert.Equal(t, 3, len(conf.TagSpecs)) assert.Equal(t, 2, len(conf.FixityAlgorithms)) // Spot checks if _, ok := conf.FileSpecs["manifest-md5.txt"]; !ok { assert.Fail(t, "FileSpec for manifest-md5.txt is missing") } if _, ok := conf.FileSpecs["manifest-sha256.txt"]; !ok { assert.Fail(t, "FileSpec for manifest-sha256.txt is missing") } if _, ok := conf.TagSpecs["Title"]; !ok { assert.Fail(t, "TagSpec for Title is missing") } if len(conf.FixityAlgorithms) > 1 { assert.Equal(t, "md5", conf.FixityAlgorithms[0]) assert.Equal(t, "sha256", conf.FixityAlgorithms[1]) } assert.Equal(t, validation.REQUIRED, conf.FileSpecs["manifest-md5.txt"].Presence) assert.Equal(t, validation.OPTIONAL, conf.FileSpecs["manifest-sha256.txt"].Presence) assert.Equal(t, "aptrust-info.txt", conf.TagSpecs["Title"].FilePath) assert.Equal(t, validation.REQUIRED, conf.TagSpecs["Title"].Presence) assert.False(t, conf.TagSpecs["Title"].EmptyOK) assert.Equal(t, 3, len(conf.TagSpecs["Access"].AllowedValues)) }
func TestIdleOriginDirect(t *testing.T) { okAddr, err := impatientProxy(0, 30*time.Second) if err != nil { assert.Fail(t, "Error starting proxy server: %s", err) } impatientAddr, err := impatientProxy(0, 50*time.Millisecond) if err != nil { assert.Fail(t, "Error starting proxy server: %s", err) } okForwardFn := func(conn net.Conn, originURL *url.URL) { var buf [400]byte chunkedReq(t, &buf, conn, originURL) assert.Contains(t, string(buf[:]), "200 OK", "should succeed") } failForwardFn := func(conn net.Conn, originURL *url.URL) { var buf [400]byte chunkedReq(t, &buf, conn, originURL) assert.Contains(t, string(buf[:]), "502 Bad Gateway", "should fail with 502") } testRoundTrip(t, okAddr, false, httpOriginServer, okForwardFn) testRoundTrip(t, impatientAddr, false, httpOriginServer, failForwardFn) }
func TestManifestInfoIsValid(t *testing.T) { // Should flag all missing items r := getReader("virginia.edu.uva-lib_2278801.tar") outputPath := strings.Replace(r.Manifest.Object.IngestTarFilePath, ".tar", "", -1) if len(outputPath) > 40 && strings.Contains(outputPath, "testdata") { defer os.RemoveAll(outputPath) } r.Manifest.Object.Identifier = "" r.Manifest.Object.BagName = "" r.Manifest.Object.Institution = "" r.Manifest.Object.IngestTarFilePath = "" r.Untar() assert.Equal(t, 5, len(r.Manifest.UntarResult.Errors)) // Should be specific about bad file path r = getReader("virginia.edu.uva-lib_2278801.tar") r.Manifest.Object.IngestTarFilePath = "/mUje9Dke0776adBx4Gq/file/does/not/exist.tar" r.Untar() if r.Manifest.UntarResult.HasErrors() == false { assert.Fail(t, "Untar WorkSummary should have errors") } else { assert.True(t, strings.Contains(r.Manifest.UntarResult.Errors[0], "does not exist")) } // If IntellectualObject is nil, we should get an // error message and not a panic. r = getReader("virginia.edu.uva-lib_2278801.tar") r.Manifest.Object = nil r.Untar() if r.Manifest.UntarResult.HasErrors() == false { assert.Fail(t, "Untar WorkSummary should have errors") } else { assert.Equal(t, "IntellectualObject is missing from manifest.", r.Manifest.UntarResult.Errors[0]) } }
func TestGetRecommendations(t *testing.T) { r1 := model.Relationship{"Tester", "tester1", "Test", "test1", "tested"} r2 := model.Relationship{"Tester", "tester2", "Test", "test1", "tested"} r3 := model.Relationship{"Tester", "tester2", "Test", "test2", "tested"} r4 := model.Relationship{"Tester", "tester2", "Test", "test3", "tested"} ProcessAddEvent(&r1) ProcessAddEvent(&r2) ProcessAddEvent(&r3) ProcessAddEvent(&r4) rq := model.RecommendationRequest{"Tester", "tester1", "Test", "tested", 5} recommendation, err := GetRecommendations(&rq) if err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } else { assert.Len(t, recommendation.Results, 2, "Unexpected size of results") } ProcessRemoveEvent(&r4) recommendation, err = GetRecommendations(&rq) if err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } else { assert.Len(t, recommendation.Results, 1, "Unexpected size of results") } ProcessRemoveEvent(&r1) ProcessRemoveEvent(&r2) ProcessRemoveEvent(&r3) }
// TestSuccessCodeAndInfoForComplexMessage sends out a complex message to the pubnub channel func TestSuccessCodeAndInfoForComplexMessage(t *testing.T) { assert := assert.New(t) stop, _ := NewVCRNonSubscribe( "fixtures/publish/successCodeAndInfoForComplexMessage", []string{"uuid"}) defer stop() pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "") channel := "successCodeAndInfoForComplexMessage" customStruct := CustomStruct{ Foo: "hi!", Bar: []int{1, 2, 3, 4, 5}, } successChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.Publish(channel, customStruct, successChannel, errorChannel) select { case msg := <-successChannel: assert.Contains(string(msg), "1,") assert.Contains(string(msg), "\"Sent\",") case err := <-errorChannel: assert.Fail(string(err)) case <-timeout(): assert.Fail("Publish timeout") } }
// Asserts that a given accumulator contains a measurment of type float64 with // specific tags within a certain distance of a given expected value. Asserts a failure // if the measurement is of the wrong type, or if no matching measurements are found // // Paramaters: // t *testing.T : Testing object to use // acc testutil.Accumulator: Accumulator to examine // measurement string : Name of the measurement to examine // expectedValue float64 : Value to search for within the measurement // delta float64 : Maximum acceptable distance of an accumulated value // from the expectedValue parameter. Useful when // floating-point arithmatic imprecision makes looking // for an exact match impractical // tags map[string]string : Tag set the found measurement must have. Set to nil to // ignore the tag set. func assertContainsTaggedFloat( t *testing.T, acc testutil.Accumulator, measurement string, expectedValue float64, delta float64, tags map[string]string, ) { for _, pt := range acc.Points { if pt.Measurement == measurement { if (tags == nil) || reflect.DeepEqual(pt.Tags, tags) { if value, ok := pt.Values["value"].(float64); ok { if (value >= expectedValue-delta) && (value <= expectedValue+delta) { // Found the point, return without failing return } } else { assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64", measurement)) } } } } assert.Fail(t, fmt.Sprintf("Could not find measurement \"%s\" with requested tags within %f of %f", measurement, delta, expectedValue)) }
// TestSubscriptionConnectStatus sends out a subscribe request to a pubnub channel // and validates the response for the connect status. func TestSubscriptionConnectStatus(t *testing.T) { assert := assert.New(t) stop, _ := NewVCRBoth( "fixtures/subscribe/connectStatus", []string{"uuid"}) defer stop() channel := "Channel_ConnectStatus" uuid := "UUID_ConnectStatus" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid) successChannel := make(chan []byte) errorChannel := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel) select { case resp := <-successChannel: response := fmt.Sprintf("%s", resp) if response != "[]" { message := "'" + channel + "' connected" assert.Contains(response, message) } case err := <-errorChannel: if !IsConnectionRefusedError(err) { assert.Fail(string(err)) } case <-timeouts(3): assert.Fail("Subscribe timeout 3s") } go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel) }
func TestGetDataCipher(t *testing.T) { assert := assert.New(t) pubnub := Pubnub{ cipherKey: "enigma", infoLogger: log.New(ioutil.Discard, "", log.Ldate|log.Ltime|log.Lshortfile), } response := `[["h5Uhyc8uf3h11w5C68QsVenCf7Llvdq5XWLa1RSgdfU=","AA9MBpymUzq/bfLCtIKFB+J6L+s3UGm6xPGh9kuXsoQ=","SfGYYp58jU2FGBNNsRk0kZ8KWRjZ6OsG3OxSySd7FF0=","ek+lrKjHCJPp5wYpxWlZcg806w/SWU5dzNYmjqDVb6o=","HrIrwvdGrm3/TM4kCf0EGl5SzcD+JqOXesWtzzc8+UA="],14610686757083461,14610686757935083]` var contents = []byte(response) var s interface{} err := json.Unmarshal(contents, &s) if err == nil { v := s.(interface{}) switch vv := v.(type) { case []interface{}: length := len(vv) if length > 0 { msgStr := pubnub.getData(vv[0], pubnub.cipherKey) //pubnub.infoLogger.Printf(msgStr) assert.Equal("[\"Test Message 5\",\"Test Message 6\",\"Test Message 7\",\"Test Message 8\",\"Test Message 9\"]", msgStr) } default: assert.Fail("default fall through") } } else { assert.Fail("Unmarshal failed") } }
// Asserts that a given accumulator contains a measurment of type float64 with // specific tags within a certain distance of a given expected value. Asserts a failure // if the measurement is of the wrong type, or if no matching measurements are found // // Paramaters: // t *testing.T : Testing object to use // acc testutil.Accumulator: Accumulator to examine // measurement string : Name of the measurement to examine // expectedValue float64 : Value to search for within the measurement // delta float64 : Maximum acceptable distance of an accumulated value // from the expectedValue parameter. Useful when // floating-point arithmatic imprecision makes looking // for an exact match impractical // tags map[string]string : Tag set the found measurement must have. Set to nil to // ignore the tag set. func assertContainsTaggedFloat( t *testing.T, acc *testutil.Accumulator, measurement string, field string, expectedValue float64, delta float64, tags map[string]string, ) { var actualValue float64 for _, pt := range acc.Points { if pt.Measurement == measurement { for fieldname, value := range pt.Fields { if fieldname == field { if value, ok := value.(float64); ok { actualValue = value if (value >= expectedValue-delta) && (value <= expectedValue+delta) { // Found the point, return without failing return } } else { assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64", measurement)) } } } } } msg := fmt.Sprintf( "Could not find measurement \"%s\" with requested tags within %f of %f, Actual: %f", measurement, delta, expectedValue, actualValue) assert.Fail(t, msg) }
// TestSpecInNamespaceGone validates that, if you delete a work spec's // namespace, attempts to use the work spec return ErrGone. func TestSpecInNamespaceGone(t *testing.T) { sts := SimpleTestSetup{ NamespaceName: "TestSpecInNamespaceGone", WorkSpecName: "spec", } sts.SetUp(t) // We are about to blow up the namespace now so there is no cleanup err := sts.Namespace.Destroy() assert.NoError(t, err) // Test a couple of basic things _, err = sts.WorkSpec.Meta(false) if err == coordinate.ErrGone { // okay } else if nsws, ok := err.(coordinate.ErrNoSuchWorkSpec); ok { assert.Equal(t, sts.WorkSpecName, nsws.Name) } else { assert.Fail(t, "unexpected error reading deleted work spec meta", "+v", err) } _, err = sts.WorkSpec.AddWorkUnit("foo", map[string]interface{}{}, coordinate.WorkUnitMeta{}) if err == coordinate.ErrGone { // okay } else if nsws, ok := err.(coordinate.ErrNoSuchWorkSpec); ok { assert.Equal(t, sts.WorkSpecName, nsws.Name) } else { assert.Fail(t, "unexpected error adding work to deleted work spec", "+v", err) } }
func TestUnsubscribe(t *testing.T) { client := create(t) defer client.TearDown() listener := client.Listener(Channel, "foobar") client.WaitFor(SubscribeEvent) publish("foobar", "heyo!") assert.Equal(t, "heyo!", string((<-listener.Messages).Data)) // Unsubscribe, then publish and listen for a second to make sure // the event doesn't come in. listener.Unsubscribe() client.WaitFor(UnsubscribeEvent) publish("foobar", "heyo!") select { case packet := <-client.OnChannel(AnyEvent): assert.Fail(t, fmt.Sprintf("Got 'some' packet after unsubscribe: %#v", packet)) case <-time.After(time.Millisecond * 100): } disrupt(client) // Make sure we don't resubscribe after a disruption. publish("foobar", "heyo!") select { case packet := <-client.OnChannel(AnyEvent): assert.Fail(t, fmt.Sprintf("Got 'some' packet after unsubscribe reconnect: %#v", packet)) case <-time.After(time.Millisecond * 100): } }
func TestDeStruct(t *testing.T) { for i, test := range tests { outputCopy, outputScalar, outputFields := deStruct(test.input) testInputPtr := reflect.ValueOf(&test.input).Pointer() outputCopyPtr := reflect.ValueOf(&outputCopy).Pointer() assertionMessage := fmt.Sprintf("Test #%d: %#v", i, test.input) assert.NotEqual(t, testInputPtr, outputCopyPtr, assertionMessage) // tests commented pending fix for https://github.com/stretchr/testify/issues/144 //assert.IsType(t, test.outputCopy, outputCopy, assertionMessage) //assert.Exactly(t, test.outputCopy, outputCopy, assertionMessage) if !reflect.DeepEqual(test.outputCopy, outputCopy) { assert.Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n"+ " != %#v (actual)", test.outputCopy, outputCopy), assertionMessage) } assert.Equal(t, test.outputScalar, outputScalar, assertionMessage) //assert.Equal(t, test.outputFields, outputFields, assertionMessage) if !reflect.DeepEqual(test.outputFields, outputFields) { assert.Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n"+ " != %#v (actual)", test.outputFields, outputFields), assertionMessage) } } }
func TestGetConnTimeout(t *testing.T) { assert := assert.New(t) pool, err := NewPool(1, 3, connCreator) if err != nil { assert.Fail("Init conn pool fail.") return } for i := 0; i < 3; i++ { conn, err := pool.Get() if err != nil { assert.Fail("Get conn fail.") } _, err = conn.Write([]byte("Test conn1")) if err != nil { assert.Fail("Write message fail.") } } _, err = pool.GetWithTimeout(time.Duration(1) * time.Second) if err != nil { fmt.Println(err) return } assert.Fail("Need get timeout error.") }
func TestActorCanStopChildren(t *testing.T) { responsePID, result := RequestResponsePID() responsePID2, result2 := RequestResponsePID() actor := Spawn(FromProducer(NewCreateChildThenStopActor)) count := 10 for i := 0; i < count; i++ { actor.Tell(CreateChildMessage{}) } actor.Tell(GetChildCountMessage2{ReplyDirectly: responsePID, ReplyAfterStop: responsePID2}) //wait for the actor to reply to the first responsePID _, err := result.ResultOrTimeout(testTimeout) if err != nil { assert.Fail(t, "timed out") return } //then send a stop command actor.Stop() //wait for the actor to stop and get the result from the stopped handler response, err := result2.ResultOrTimeout(testTimeout) if err != nil { assert.Fail(t, "timed out") return } //we should have 0 children when the actor is stopped assert.Equal(t, 0, response.(GetChildCountReplyMessage).ChildCount) }
func (suite *AnnoyTestSuite) TestFileHandling() { index := annoyindex.NewAnnoyIndexAngular(3) index.AddItem(0, []float32{0, 0, 1}) index.AddItem(1, []float32{0, 1, 0}) index.AddItem(2, []float32{1, 0, 0}) index.Build(10) index.Save("go_test.ann") info, err := os.Stat("go_test.ann") if err != nil { assert.Fail(suite.T(), "Failed to create file, file not found") } if info.Size() == 0 { assert.Fail(suite.T(), "Failed to create file, file size zero") } annoyindex.DeleteAnnoyIndexAngular(index) index = annoyindex.NewAnnoyIndexAngular(3) if ret := index.Load("go_test.ann"); ret == false { assert.Fail(suite.T(), "Failed to load file") } annoyindex.DeleteAnnoyIndexAngular(index) os.Remove("go_test.ann") }
func TestGetDataCipherNonEncSingle(t *testing.T) { assert := assert.New(t) pubnub := Pubnub{ cipherKey: "enigma", infoLogger: log.New(ioutil.Discard, "", log.Ldate|log.Ltime|log.Lshortfile), } response := "[\"Test Message 5\",14610686757083461,14610686757935083]" var contents = []byte(response) var s interface{} err := json.Unmarshal(contents, &s) if err == nil { v := s.(interface{}) switch vv := v.(type) { case []interface{}: length := len(vv) if length > 0 { msgStr := pubnub.parseInterface(vv, pubnub.cipherKey) //pubnub.infoLogger.Printf(msgStr) assert.Equal("[\"Test Message 5\",1.461068675708346e+16,1.4610686757935084e+16]", msgStr) } default: assert.Fail("default fall through") } } else { assert.Fail("Unmarshal failed %s", err.Error()) } }
func TestCreateOrIncRelationship(t *testing.T) { db := GetGraph() node1, _ := createNode(db, "graphtest2_1", "node1", t) node2, _ := createNode(db, "graphtest2_2", "node2", t) if node1 != nil && node2 != nil { r := model.Relationship{"graphtest2_1", "node1", "graphtest2_2", "node2", "tested"} if err := CreateOrIncRelationship(db, &r); err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } freq := getFrequencyRelations(db, &r, t) if freq > -1 { assert.Equal(t, 1, freq, "Unexpected value for frequency (times the same relationship happened)") } if err := CreateOrIncRelationship(db, &r); err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } freq = getFrequencyRelations(db, &r, t) if freq > -1 { assert.Equal(t, 2, freq, "Unexpected value for frequency (times the same relationship happened)") } deleteNode(db, node1) deleteNode(db, node2) } }
// TestSuccessCodeAndInfoWithEncryption sends out an encrypted // message to the pubnub channel func TestSuccessCodeAndInfoWithEncryption(t *testing.T) { assert := assert.New(t) stop, _ := NewVCRNonSubscribe( "fixtures/publish/successCodeAndInfoWithEncryption", []string{"uuid"}) defer stop() pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "enigma", false, "") channel := "successCodeAndInfo" message := "Pubnub API Usage Example" successChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.Publish(channel, message, successChannel, errorChannel) select { case msg := <-successChannel: assert.Contains(string(msg), "1,") assert.Contains(string(msg), "\"Sent\",") case err := <-errorChannel: assert.Fail(string(err)) case <-timeout(): assert.Fail("Publish timeout") } }
// TestTar tests the tar.go file func TestTar(t *testing.T) { var err error var data map[string][]byte _, path, _, _ := runtime.Caller(0) testDataDir := "/testdata" for _, filename := range []string{"utils_test.tar.gz", "utils_test.tar.bz2", "utils_test.tar.xz", "utils_test.tar"} { testArchivePath := filepath.Join(filepath.Dir(path), testDataDir, filename) // Extract non compressed data data, err = SelectivelyExtractArchive(bytes.NewReader([]byte("that string does not represent a tar or tar-gzip file")), "", []string{}, 0) assert.Error(t, err, "Extracting non compressed data should return an error") // Extract an archive f, _ := os.Open(testArchivePath) defer f.Close() data, err = SelectivelyExtractArchive(f, "", []string{"test/"}, 0) assert.Nil(t, err) if c, n := data["test/test.txt"]; !n { assert.Fail(t, "test/test.txt should have been extracted") } else { assert.NotEqual(t, 0, len(c) > 0, "test/test.txt file is empty") } if _, n := data["test.txt"]; n { assert.Fail(t, "test.txt should not be extracted") } // File size limit f, _ = os.Open(testArchivePath) defer f.Close() data, err = SelectivelyExtractArchive(f, "", []string{"test"}, 50) assert.Equal(t, ErrExtractedFileTooBig, err) } }
// These good bags are from the old Bagman test suite. We have to make sure they // pass here, so we know validation is backwards-compatible. func TestValidate_GoodBags(t *testing.T) { goodBags := []string{ "example.edu.multipart.b01.of02.tar", "example.edu.multipart.b02.of02.tar", "example.edu.sample_good.tar", } bagValidationConfig, err := getValidationConfig() if err != nil { assert.Fail(t, "Could not load BagValidationConfig: %s", err.Error()) } optionalFileSpec := validation.FileSpec{Presence: "OPTIONAL"} bagValidationConfig.FileSpecs["tagmanifest-md5.txt"] = optionalFileSpec _, filename, _, _ := runtime.Caller(0) dir := filepath.Dir(filename) for _, goodBag := range goodBags { pathToBag, err := filepath.Abs(path.Join(dir, "..", "testdata", "unit_test_bags", goodBag)) validator, err := validation.NewBagValidator(pathToBag, bagValidationConfig) if err != nil { assert.Fail(t, "NewBagValidator returned unexpected error: %s", err.Error()) } result := validator.Validate() require.NotNil(t, result.IntellectualObject, goodBag) assert.NotEmpty(t, result.IntellectualObject.GenericFiles, goodBag) assert.Empty(t, result.IntellectualObject.IngestErrorMessage, goodBag) assert.False(t, result.ParseSummary.HasErrors(), goodBag) } }
// TestServerTime calls the GetTime method of the messaging to test the time func TestServerTime(t *testing.T) { stop, _ := NewVCRNonSubscribe("fixtures/time", []string{}) defer stop() pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "testTime") assert := assert.New(t) successChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.GetTime(successChannel, errorChannel) select { case value := <-successChannel: response := string(value) timestamp, err := strconv.Atoi(strings.Trim(response, "[]\n")) if err != nil { assert.Fail(err.Error()) } assert.NotZero(timestamp) case err := <-errorChannel: assert.Fail(string(err)) case <-timeouts(10): assert.Fail("Getting server timestamp timeout") } }
// A valid bag should have no errors. func TestBagValidator_ValidBag(t *testing.T) { bagValidationConfig, err := getValidationConfig() if err != nil { assert.Fail(t, "Could not load BagValidationConfig: %s", err.Error()) } _, filename, _, _ := runtime.Caller(0) dir := filepath.Dir(filename) pathToBag, err := filepath.Abs(path.Join(dir, "..", "testdata", "unit_test_bags", "example.edu.tagsample_good.tar")) validator, err := validation.NewBagValidator(pathToBag, bagValidationConfig) if err != nil { assert.Fail(t, "NewBagValidator returned unexpected error: %s", err.Error()) } result := validator.Validate() assert.NotNil(t, result.IntellectualObject) assert.Equal(t, 16, len(result.IntellectualObject.GenericFiles)) assert.Empty(t, result.IntellectualObject.IngestErrorMessage) assert.False(t, result.ParseSummary.HasErrors()) assert.NotNil(t, result.ValidationSummary) require.False(t, result.ValidationSummary.HasErrors()) for _, gf := range result.IntellectualObject.GenericFiles { assert.NotEmpty(t, gf.IngestSha256VerifiedAt) assert.NotEmpty(t, gf.IngestMd5VerifiedAt) } }
// TestUnsubscribe will subscribe to a pubnub channel and then send an unsubscribe request // The response should contain 'unsubscribed' func TestUnsubscribeChannel(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/unsubscribe/channel", []string{"uuid"}) defer stop() channel := "Channel_UnsubscribeChannel" uuid := "UUID_UnsubscribeChannel" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid) subscribeSuccessChannel := make(chan []byte) subscribeErrorChannel := make(chan []byte) unSubscribeSuccessChannel := make(chan []byte) unSubscribeErrorChannel := make(chan []byte) go pubnubInstance.Subscribe(channel, "", subscribeSuccessChannel, false, subscribeErrorChannel) select { case msg := <-subscribeSuccessChannel: val := string(msg) assert.Equal(val, fmt.Sprintf( "[1, \"Subscription to channel '%s' connected\", \"%s\"]", channel, channel)) case err := <-subscribeErrorChannel: assert.Fail(string(err)) } sleep(2) go pubnubInstance.Unsubscribe(channel, unSubscribeSuccessChannel, unSubscribeErrorChannel) select { case msg := <-unSubscribeSuccessChannel: val := string(msg) assert.Equal(val, fmt.Sprintf( "[1, \"Subscription to channel '%s' unsubscribed\", \"%s\"]", channel, channel)) case err := <-unSubscribeErrorChannel: assert.Fail(string(err)) } select { case ev := <-unSubscribeSuccessChannel: var event messaging.PresenceResonse err := json.Unmarshal(ev, &event) if err != nil { assert.Fail(err.Error()) } assert.Equal("leave", event.Action) assert.Equal(200, event.Status) case err := <-unSubscribeErrorChannel: assert.Fail(string(err)) } // pubnubInstance.CloseExistingConnection() }
func TestSetUserStateGlobalHereNow(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/presence/setUserStateGlobalHereNow", []string{"uuid"}) defer stop() channel := "Channel_SetUserStateGlobalHereNow" uuid := "UUID_SetUserStateGlobalHereNow" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, uuid) key := "testkey" val := "testval" successChannel := make(chan []byte) errorChannel := make(chan []byte) successSet := make(chan []byte) errorSet := make(chan []byte) successGet := make(chan []byte) errorGet := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel) ExpectConnectedEvent(t, channel, "", successChannel, errorChannel) go pubnubInstance.SetUserStateKeyVal(channel, key, val, successSet, errorSet) select { case value := <-successSet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\"}", key, val) assert.Contains(actual, expectedSubstring) case err := <-errorSet: assert.Fail("Failed to set state", string(err)) case <-messaging.Timeout(): assert.Fail("Set state timeout") } sleep(PresenceServerTimeoutLower) go pubnubInstance.GlobalHereNow(true, true, successGet, errorGet) select { case value := <-successGet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\"}", key, val) assert.Contains(actual, expectedSubstring) assert.Contains(actual, pubnubInstance.GetUUID()) case err := <-errorSet: assert.Fail("Failed to get state", string(err)) case <-messaging.Timeout(): assert.Fail("Get state timeout") } go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel) }
func TestEventStreamEventsReceived(t *testing.T) { if !assert.True(t, len(testCases) > 1, "must have at least 2 test cases to end prematurely") { return } clientCfg := NewDefaultConfig() config := configContainer{ client: &clientCfg, } config.client.EventsTransport = EventsTransportSSE endpoint := newFakeMarathonEndpoint(t, &config) defer endpoint.Close() events, err := endpoint.Client.AddEventsListener(EventIDApplications | EventIDDeploymentInfo) assert.NoError(t, err) almostAllTestCases := testCases[:len(testCases)-1] finalTestCase := testCases[len(testCases)-1] // Publish all but one test event. for _, testCase := range almostAllTestCases { endpoint.Server.PublishEvent(testCase.source) } // Receive test events. for i := 0; i < len(almostAllTestCases); i++ { select { case event := <-events: tc := testCases.find(event.Name) if !assert.NotNil(t, tc, "received unknown event: %s", event.Name) { continue } assert.Equal(t, tc.expectation, event.Event) case <-time.After(eventPublishTimeout): assert.Fail(t, "did not receive event in time") } } // Publish last test event that we do not intend to consume anymore. endpoint.Server.PublishEvent(finalTestCase.source) // Give event stream some time to buffer another event. time.Sleep(eventPublishTimeout) // Trigger done channel closure. endpoint.Client.RemoveEventsListener(events) // Give pending goroutine time to consume done signal. time.Sleep(eventPublishTimeout) // Validate that channel is closed. select { case _, more := <-events: assert.False(t, more, "should not have received another event") default: assert.Fail(t, "channel was not closed") } }
func TestRemoveRelationship(t *testing.T) { r := model.Relationship{"graphtest3_1", "node1", "graphtest3_2", "node2", "tested"} if err := CreateNodesAndRelationship(&r); err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } if err := RemoveRelationship(&r); err != nil { assert.Fail(t, "Unexpected error: "+err.Error()) } }