github.com/weaviate/weaviate@v1.24.6/test/acceptance/telemetry/logging_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package test 13 14 // TODO gh-1232 remove 15 // // This test covers the Telemetry process. Weaviate should log each time its endpoints are accessed, 16 // // and it should send these logs to an endpoint. In the testing config environment this endpoint is 17 // // the mock api's address. This test sends a request to Weaviate to ensure at least one log *should* 18 // // exist, waits a few seconds to ensure the log *should* be sent to the mock endpoint and then retrieves 19 // // the most recently received log from the mock endpoint. The test then decodes the response and 20 // // validates its structure. 21 // // 22 // // controlled by setup_test.go 23 // func createActionLogging(t *testing.T) { 24 // // send a request 25 // sendCreateActionRequest(t) 26 27 // // wait for the log to be posted 28 // time.Sleep(7 * time.Second) 29 30 // result := retrieveLogFromMockEndpoint(t) 31 32 // if result != nil { 33 // interpretedResult := interpretResult(t, result) 34 // if interpretedResult != nil { 35 // _, namePresent := interpretedResult["n"] 36 // _, typePresent := interpretedResult["t"] 37 // _, identifierPresent := interpretedResult["i"] 38 // _, amountPresent := interpretedResult["a"] 39 // _, whenPresent := interpretedResult["w"] 40 41 // assert.Equal(t, true, namePresent) 42 // assert.Equal(t, true, typePresent) 43 // assert.Equal(t, true, identifierPresent) 44 // assert.Equal(t, true, amountPresent) 45 // assert.Equal(t, true, whenPresent) 46 47 // } 48 // } 49 // } 50 51 // // The sendCreateActionRequest acceptance test is copied here to ensure at least one request should be 52 // // logged when we check the mock api's most recently received request. These assertions should pass 53 // // regardless of the rest of the Telemetry test passing. 54 // func sendCreateActionRequest(t *testing.T) { 55 // // Set all action values to compare 56 // actionTestString := "Test string" 57 // actionTestInt := 1 58 // actionTestBoolean := true 59 // actionTestNumber := 1.337 60 // actionTestDate := "2017-10-06T08:15:30+01:00" 61 62 // params := actions.NewActionsCreateParams().WithBody( 63 // &models.Action{ 64 // Class: "MonitoringTestAction", 65 // Schema: map[string]interface{}{ 66 // "testString": actionTestString, 67 // "testWholeNumber": actionTestInt, 68 // "testTrueFalse": actionTestBoolean, 69 // "testNumber": actionTestNumber, 70 // "testDateTime": actionTestDate, 71 // }, 72 // }) 73 74 // resp, err := helper.Client(t).Actions.ActionsCreate(params, nil) 75 76 // // Ensure that the response is OK. 77 // helper.AssertRequestOk(t, resp, err, func() { 78 // action := resp.Payload 79 // assert.Regexp(t, strfmt.UUIDPattern, action.ID) 80 81 // schema, ok := action.Schema.(map[string]interface{}) 82 // if !ok { 83 // t.Fatal("The returned schema is not an JSON object") 84 // } 85 86 // testWholeNumber, _ := schema["testWholeNumber"].(json.Number).Int64() 87 // testNumber, _ := schema["testNumber"].(json.Number).Float64() 88 89 // // Check whether the returned information is the same as the data added. 90 // assert.Equal(t, actionTestString, schema["testString"]) 91 // assert.Equal(t, actionTestInt, int(testWholeNumber)) 92 // assert.Equal(t, actionTestBoolean, schema["testTrueFalse"]) 93 // assert.Equal(t, actionTestNumber, testNumber) 94 // assert.Equal(t, actionTestDate, schema["testDateTime"]) 95 // }) 96 // } 97 98 // // retrieveLogFromMockEndpoint retrieves the most recently received log from the mock api. 99 // func retrieveLogFromMockEndpoint(t *testing.T) []byte { 100 // req, err := http.NewRequest("GET", "http://localhost:8087/mock/last", nil) 101 // req.Close = true 102 // assert.Equal(t, nil, err) 103 104 // client := &http.Client{} 105 // resp, err := client.Do(req) 106 // if err == nil { 107 // body, _ := io.ReadAll(resp.Body) 108 // defer resp.Body.Close() 109 // return body 110 // } 111 // if err != nil { 112 // urlError, ok := err.(*url.Error) 113 // if ok { 114 // assert.Equal(t, nil, urlError.Op) 115 // } 116 // } 117 118 // return nil 119 // } 120 121 // // interpretResult converts the received cbor-encoded log to a []map[string]interface. 122 // func interpretResult(t *testing.T, resultBody []byte) map[string]interface{} { 123 // decoded := make([]map[string]interface{}, 1) 124 // cborHandle := new(codec.CborHandle) 125 // encoder := codec.NewDecoderBytes(resultBody, cborHandle) 126 // err := encoder.Decode(&decoded) 127 128 // require.Equal(t, nil, err) 129 // return decoded[0] 130 // }