github.com/simpleiot/simpleiot@v0.18.3/client/db_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
    12  
    13  	"github.com/simpleiot/simpleiot/client"
    14  	"github.com/simpleiot/simpleiot/data"
    15  	"github.com/simpleiot/simpleiot/server"
    16  )
    17  
    18  func checkPort(host string, port string) error {
    19  	timeout := time.Second
    20  	conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
    21  	if err != nil {
    22  		return fmt.Errorf("Connecting error: %v", err)
    23  	}
    24  	if conn != nil {
    25  		defer conn.Close()
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  func TestDb(t *testing.T) {
    32  	// check if there is an influxdb server running, IE skip this test in CI runs
    33  	err := checkPort("localhost", "8086")
    34  	if err != nil {
    35  		fmt.Println("Error opening influx port, skipping TestDb: ", err)
    36  		t.Skip("Error opening Influx port")
    37  	}
    38  
    39  	authToken := os.Getenv("INFLUX_AUTH_TOKEN")
    40  	if authToken == "" {
    41  		t.Skip("Environment variable INFLUX_AUTH_TOKEN is not set")
    42  	}
    43  
    44  	// Start up a SIOT test server for this test
    45  	nc, root, stop, err := server.TestServer()
    46  	_ = nc
    47  
    48  	if err != nil {
    49  		t.Fatal("Error starting test server: ", err)
    50  	}
    51  
    52  	defer stop()
    53  
    54  	dbConfig := client.Db{
    55  		ID:          "ID-db",
    56  		Parent:      root.ID,
    57  		Description: "influxdb",
    58  		URI:         "http://localhost:8086",
    59  		Org:         "siot-test",
    60  		Bucket:      "test",
    61  		AuthToken:   authToken,
    62  	}
    63  
    64  	// set up Db client
    65  	err = client.SendNodeType(nc, dbConfig, "test")
    66  	if err != nil {
    67  		t.Fatal("Error sending node: ", err)
    68  	}
    69  
    70  	// connect to influx
    71  	iClient := influxdb2.NewClient(dbConfig.URI, dbConfig.AuthToken)
    72  	iQuery := iClient.QueryAPI(dbConfig.Org)
    73  	_ = iQuery
    74  
    75  	// wait for client to start
    76  	time.Sleep(time.Millisecond * 100)
    77  
    78  	// write a point and then see if it shows up in influxdb
    79  	err = client.SendNodePoint(nc, dbConfig.ID,
    80  		data.Point{Type: data.PointTypeDescription, Text: "updated description", Origin: "test"}, true)
    81  
    82  	if err != nil {
    83  		t.Fatal("Error sending points")
    84  	}
    85  
    86  	query := fmt.Sprintf(`
    87  		from(bucket:"test")
    88  		  |> range(start: -15m)
    89  		  |> filter(fn: (r) => r._measurement == "points" and r.nodeID == "%v" and
    90  		  	r.type == "description" and r._field == "text")
    91  		  |> last()
    92  	`, dbConfig.ID)
    93  
    94  	// points are batched by the influx client and can take up to 1s to be written
    95  	time.Sleep(time.Second * 1)
    96  
    97  	result, err := iQuery.Query(context.Background(), query)
    98  	if err != nil {
    99  		t.Fatal("influx query failed: ", err)
   100  	}
   101  
   102  	var pTime time.Time
   103  	var pValue string
   104  
   105  	for result.Next() {
   106  		r := result.Record()
   107  		pTime = r.Time()
   108  		pValue = r.Value().(string)
   109  	}
   110  
   111  	err = result.Err()
   112  
   113  	if err != nil {
   114  		t.Fatal("influx result error: ", err)
   115  	}
   116  
   117  	if time.Since(pTime) > time.Second*4 {
   118  		t.Fatal("Did not get a point recently: ", time.Since(pTime), pTime)
   119  	}
   120  
   121  	if pValue != "updated description" {
   122  		t.Fatal("Point value not correct")
   123  	}
   124  }