@@ -2,6 +2,7 @@ package grafana
22
33import (
44 "fmt"
5+ "reflect"
56 "strconv"
67 "testing"
78
@@ -566,6 +567,133 @@ func TestAccDataSource_basic(t *testing.T) {
566567 }
567568}
568569
570+ func TestDatasourceMigrationV0 (t * testing.T ) {
571+ cases := []struct {
572+ name string
573+ state map [string ]interface {}
574+ expected map [string ]interface {}
575+ }{
576+ {
577+ name : "no json data" ,
578+ state : map [string ]interface {}{
579+ "name" : "test" ,
580+ "type" : "prometheus" ,
581+ },
582+ expected : map [string ]interface {}{
583+ "name" : "test" ,
584+ "type" : "prometheus" ,
585+ },
586+ },
587+ {
588+ name : "no tsdb fields" ,
589+ state : map [string ]interface {}{
590+ "name" : "test" ,
591+ "type" : "prometheus" ,
592+ "json_data" : []map [string ]interface {}{
593+ {
594+ "url" : "http://localhost:9090" ,
595+ },
596+ },
597+ },
598+ expected : map [string ]interface {}{
599+ "name" : "test" ,
600+ "type" : "prometheus" ,
601+ "json_data" : []map [string ]interface {}{
602+ {
603+ "url" : "http://localhost:9090" ,
604+ },
605+ },
606+ },
607+ },
608+ {
609+ name : "nil or empty tsdb fields" ,
610+ state : map [string ]interface {}{
611+ "name" : "test" ,
612+ "type" : "test" ,
613+ "json_data" : []map [string ]interface {}{
614+ {
615+ "tsdb_version" : "" ,
616+ "tsdb_resolution" : nil ,
617+ "url" : "http://localhost:9090" ,
618+ },
619+ },
620+ },
621+ expected : map [string ]interface {}{
622+ "name" : "test" ,
623+ "type" : "test" ,
624+ "json_data" : []map [string ]interface {}{
625+ {
626+ "tsdb_version" : 0 ,
627+ "tsdb_resolution" : 0 ,
628+ "url" : "http://localhost:9090" ,
629+ },
630+ },
631+ },
632+ },
633+ {
634+ name : "already int" ,
635+ state : map [string ]interface {}{
636+ "name" : "test" ,
637+ "type" : "test" ,
638+ "json_data" : []map [string ]interface {}{
639+ {
640+ "tsdb_version" : 0 ,
641+ "tsdb_resolution" : 2 ,
642+ "url" : "http://localhost:9090" ,
643+ },
644+ },
645+ },
646+ expected : map [string ]interface {}{
647+ "name" : "test" ,
648+ "type" : "test" ,
649+ "json_data" : []map [string ]interface {}{
650+ {
651+ "tsdb_version" : 0 ,
652+ "tsdb_resolution" : 2 ,
653+ "url" : "http://localhost:9090" ,
654+ },
655+ },
656+ },
657+ },
658+ {
659+ name : "migration" ,
660+ state : map [string ]interface {}{
661+ "name" : "test" ,
662+ "type" : "test" ,
663+ "json_data" : []map [string ]interface {}{
664+ {
665+ "tsdb_version" : "0" ,
666+ "tsdb_resolution" : "2" ,
667+ "url" : "http://localhost:9090" ,
668+ },
669+ },
670+ },
671+ expected : map [string ]interface {}{
672+ "name" : "test" ,
673+ "type" : "test" ,
674+ "json_data" : []map [string ]interface {}{
675+ {
676+ "tsdb_version" : 0 ,
677+ "tsdb_resolution" : 2 ,
678+ "url" : "http://localhost:9090" ,
679+ },
680+ },
681+ },
682+ },
683+ }
684+ for _ , tc := range cases {
685+ t .Run (tc .name , func (t * testing.T ) {
686+ actual , err := resourceDataSourceV0Upgrader .Upgrade (nil , tc .expected , nil )
687+ if err != nil {
688+ t .Fatal (err )
689+ }
690+ if ! reflect .DeepEqual (actual , tc .expected ) {
691+ t .Errorf ("Expected %#v, got %#v" , tc .expected , actual )
692+ }
693+ })
694+ }
695+ }
696+
569697func testAccDataSourceCheckExists (rn string , dataSource * gapi.DataSource ) resource.TestCheckFunc {
570698 return func (s * terraform.State ) error {
571699 rs , ok := s .RootModule ().Resources [rn ]
0 commit comments