Merge lp:~fwereade/juju-core/reinstate-relation-watch-tests into lp:~go-bot/juju-core/trunk

Proposed by William Reade
Status: Merged
Approved by: William Reade
Approved revision: no longer in the source branch.
Merged at revision: 1735
Proposed branch: lp:~fwereade/juju-core/reinstate-relation-watch-tests
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 518 lines (+484/-3)
1 file modified
state/relationunit_test.go (+484/-3)
To merge this branch: bzr merge lp:~fwereade/juju-core/reinstate-relation-watch-tests
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+183189@code.launchpad.net

Commit message

state: reinstate accidentally-dropped tests

Aside from removal of the tests for the Joined field (which is no longer sent,
because it's redundant (preexisting change)), this was a mechanical rename
job from the original. It's reassuring to observe that behaviour had not
otherwise changed detectably in the interim.

https://codereview.appspot.com/13401045/

Description of the change

state: reinstate accidentally-dropped tests

Aside from removal of the tests for the Joined field (which is no longer sent,
because it's redundant (preexisting change)), this was a mechanical rename
job from the original. It's reassuring to observe that behaviour had not
otherwise changed detectably in the interim.

https://codereview.appspot.com/13401045/

To post a comment you must log in.
Revision history for this message
William Reade (fwereade) wrote :

Reviewers: mp+183189_code.launchpad.net,

Message:
Please take a look.

Description:
state: reinstate accidentally-dropped tests

Aside from removal of the tests for the Joined field (which is no longer
sent,
because it's redundant (preexisting change)), this was a mechanical
rename
job from the original. It's reassuring to observe that behaviour had not
otherwise changed detectably in the interim.

https://code.launchpad.net/~fwereade/juju-core/reinstate-relation-watch-tests/+merge/183189

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/13401045/

Affected files:
   A [revision details]
   M state/relationunit_test.go

Revision history for this message
Roger Peppe (rogpeppe) wrote :

On 2013/08/30 14:30:20, fwereade wrote:
> Please take a look.

assuming this is mechanical and pre-reviewed, LGTM.

https://codereview.appspot.com/13401045/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'state/relationunit_test.go'
--- state/relationunit_test.go 2013-08-20 15:29:55 +0000
+++ state/relationunit_test.go 2013-08-30 14:19:19 +0000
@@ -4,7 +4,9 @@
4package state_test4package state_test
55
6import (6import (
7 "fmt"
7 "sort"8 "sort"
9 "strconv"
8 "time"10 "time"
911
10 gc "launchpad.net/gocheck"12 gc "launchpad.net/gocheck"
@@ -14,9 +16,11 @@
14 "launchpad.net/juju-core/state"16 "launchpad.net/juju-core/state"
15 "launchpad.net/juju-core/state/testing"17 "launchpad.net/juju-core/state/testing"
16 coretesting "launchpad.net/juju-core/testing"18 coretesting "launchpad.net/juju-core/testing"
17 "launchpad.net/juju-core/testing/checkers"19 jc "launchpad.net/juju-core/testing/checkers"
18)20)
1921
22type RUs []*state.RelationUnit
23
20type RelationUnitSuite struct {24type RelationUnitSuite struct {
21 ConnSuite25 ConnSuite
22}26}
@@ -325,7 +329,7 @@
325 c.Assert(err, gc.IsNil)329 c.Assert(err, gc.IsNil)
326 s.assertInScope(c, pr.ru1, false)330 s.assertInScope(c, pr.ru1, false)
327 err = rel.Refresh()331 err = rel.Refresh()
328 c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)332 c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
329333
330 // The settings were not themselves actually deleted yet...334 // The settings were not themselves actually deleted yet...
331 assertSettings()335 assertSettings()
@@ -741,4 +745,481 @@
741 return u, ru745 return u, ru
742}746}
743747
744type RUs []*state.RelationUnit748type WatchScopeSuite struct {
749 ConnSuite
750}
751
752var _ = gc.Suite(&WatchScopeSuite{})
753
754func (s *WatchScopeSuite) TestPeer(c *gc.C) {
755 // Create a service and get a peer relation.
756 riak, err := s.State.AddService("riak", s.AddTestingCharm(c, "riak"))
757 c.Assert(err, gc.IsNil)
758 riakEP, err := riak.Endpoint("ring")
759 c.Assert(err, gc.IsNil)
760 rels, err := riak.Relations()
761 c.Assert(err, gc.IsNil)
762 c.Assert(rels, gc.HasLen, 1)
763 rel := rels[0]
764
765 // Add some units to the service and set their private addresses; get
766 // the relevant RelationUnits.
767 // (Private addresses should be set by their unit agents on
768 // startup; this test does not include that, but Join expects
769 // the information to be available, and uses it to populate the
770 // relation settings node.)
771 addUnit := func(i int) *state.RelationUnit {
772 unit, err := riak.AddUnit()
773 c.Assert(err, gc.IsNil)
774 err = unit.SetPrivateAddress(fmt.Sprintf("riak%d.example.com", i))
775 c.Assert(err, gc.IsNil)
776 ru, err := rel.Unit(unit)
777 c.Assert(err, gc.IsNil)
778 c.Assert(ru.Endpoint(), gc.Equals, riakEP)
779 return ru
780 }
781 ru0 := addUnit(0)
782 ru1 := addUnit(1)
783 ru2 := addUnit(2)
784
785 // ---------- Single unit ----------
786
787 // Start watching the relation from the perspective of the first unit.
788 w0 := ru0.Watch()
789 defer testing.AssertStop(c, w0)
790 s.assertChange(c, w0, nil, nil)
791 s.assertNoChange(c, w0)
792
793 // Join the first unit to the relation, and change the settings, and
794 // check that nothing apparently happens.
795 err = ru0.EnterScope(nil)
796 c.Assert(err, gc.IsNil)
797 s0 := changeSettings(c, ru0)
798 s.assertNoChange(c, w0)
799
800 // ---------- Two units ----------
801
802 // Now join another unit to the relation...
803 err = ru1.EnterScope(nil)
804 c.Assert(err, gc.IsNil)
805
806 // ...and check that the first relation unit sees the change.
807 s1, err := ru1.Settings()
808 c.Assert(err, gc.IsNil)
809 expectChanged := map[string]map[string]interface{}{
810 "riak/1": s1.Map(),
811 }
812 s.assertChange(c, w0, expectChanged, nil)
813 s.assertNoChange(c, w0)
814
815 // Join again, check it's a no-op.
816 err = ru1.EnterScope(nil)
817 c.Assert(err, gc.IsNil)
818 s.assertNoChange(c, w0)
819
820 // Start watching the relation from the perspective of the second unit,
821 // and check that it sees the right state.
822 w1 := ru1.Watch()
823 defer testing.AssertStop(c, w1)
824 expectChanged = map[string]map[string]interface{}{
825 "riak/0": s0.Map(),
826 }
827 s.assertChange(c, w1, expectChanged, nil)
828 s.assertNoChange(c, w1)
829
830 // ---------- Three units ----------
831
832 // Whoa, it works. Ok, check the third unit's opinion of the state.
833 w2 := ru2.Watch()
834 defer testing.AssertStop(c, w2)
835 expectChanged = map[string]map[string]interface{}{
836 "riak/0": s0.Map(),
837 "riak/1": s1.Map(),
838 }
839 s.assertChange(c, w2, expectChanged, nil)
840 s.assertNoChange(c, w2)
841
842 // Join the third unit, and check the first and second units see it.
843 err = ru2.EnterScope(nil)
844 c.Assert(err, gc.IsNil)
845 s2, err := ru2.Settings()
846 c.Assert(err, gc.IsNil)
847 expectChanged = map[string]map[string]interface{}{
848 "riak/2": s2.Map(),
849 }
850 s.assertChange(c, w0, expectChanged, nil)
851 s.assertNoChange(c, w0)
852 s.assertChange(c, w1, expectChanged, nil)
853 s.assertNoChange(c, w1)
854
855 // Change the second unit's settings, and check that only
856 // the first and third see changes.
857 s1 = changeSettings(c, ru1)
858 s.assertNoChange(c, w1)
859 expectChanged = map[string]map[string]interface{}{
860 "riak/1": s1.Map(),
861 }
862 s.assertChange(c, w0, expectChanged, nil)
863 s.assertNoChange(c, w0)
864 s.assertChange(c, w2, expectChanged, nil)
865 s.assertNoChange(c, w2)
866
867 // ---------- Two units again ----------
868
869 // Depart the second unit, and check that the first and third detect it.
870 err = ru1.LeaveScope()
871 c.Assert(err, gc.IsNil)
872 expectDeparted := []string{"riak/1"}
873 s.assertChange(c, w0, nil, expectDeparted)
874 s.assertNoChange(c, w0)
875 s.assertChange(c, w2, nil, expectDeparted)
876 s.assertNoChange(c, w2)
877
878 // Change its settings, and check the others don't observe anything.
879 s1 = changeSettings(c, ru1)
880 s.assertNoChange(c, w0)
881 s.assertNoChange(c, w2)
882
883 // Check no spurious events showed up on the second unit's watch, and check
884 // it closes cleanly.
885 s.assertNoChange(c, w1)
886 testing.AssertStop(c, w1)
887
888 // OK, we're done here. Cleanup, and error detection during same,
889 // will be handled by the deferred kill/stop calls. Phew.
890}
891
892func (s *WatchScopeSuite) TestProviderRequirerGlobal(c *gc.C) {
893 // Create a pair of services and a relation between them.
894 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql"))
895 c.Assert(err, gc.IsNil)
896 mysqlEP, err := mysql.Endpoint("server")
897 c.Assert(err, gc.IsNil)
898 wordpress, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
899 c.Assert(err, gc.IsNil)
900 wordpressEP, err := wordpress.Endpoint("db")
901 c.Assert(err, gc.IsNil)
902 rel, err := s.State.AddRelation(mysqlEP, wordpressEP)
903 c.Assert(err, gc.IsNil)
904
905 // Add some units to the services and set their private addresses.
906 addUnit := func(srv *state.Service, sub string, ep state.Endpoint) *state.RelationUnit {
907 unit, err := srv.AddUnit()
908 c.Assert(err, gc.IsNil)
909 ru, err := rel.Unit(unit)
910 c.Assert(err, gc.IsNil)
911 c.Assert(ru.Endpoint(), gc.Equals, ep)
912 return ru
913 }
914 msru0 := addUnit(mysql, "ms0", mysqlEP)
915 msru1 := addUnit(mysql, "ms1", mysqlEP)
916 wpru0 := addUnit(wordpress, "wp0", wordpressEP)
917 wpru1 := addUnit(wordpress, "wp1", wordpressEP)
918
919 // ---------- Single role active ----------
920
921 // Watch the relation from the perspective of the first provider unit and
922 // check initial event.
923 msw0 := msru0.Watch()
924 defer testing.AssertStop(c, msw0)
925 s.assertChange(c, msw0, nil, nil)
926 s.assertNoChange(c, msw0)
927
928 // Join the unit to the relation, change its settings, and check that
929 // nothing apparently happens.
930 err = msru0.EnterScope(nil)
931 c.Assert(err, gc.IsNil)
932 mss0 := changeSettings(c, msru0)
933 s.assertNoChange(c, msw0)
934
935 // Join the second provider unit, start its watch, and check what it thinks the
936 // state of the relation is.
937 err = msru1.EnterScope(nil)
938 c.Assert(err, gc.IsNil)
939 msw1 := msru1.Watch()
940 defer testing.AssertStop(c, msw1)
941 s.assertChange(c, msw1, nil, nil)
942 s.assertNoChange(c, msw1)
943
944 // Change the unit's settings, and check that neither provider unit
945 // observes any change.
946 mss1 := changeSettings(c, msru1)
947 s.assertNoChange(c, msw1)
948 s.assertNoChange(c, msw0)
949
950 // ---------- Two roles active ----------
951
952 // Start watches from both requirer units' perspectives, and check that
953 // they see the provider units.
954 expectChanged := map[string]map[string]interface{}{
955 "mysql/0": mss0.Map(),
956 "mysql/1": mss1.Map(),
957 }
958 wpw0 := wpru0.Watch()
959 defer testing.AssertStop(c, wpw0)
960 s.assertChange(c, wpw0, expectChanged, nil)
961 s.assertNoChange(c, wpw0)
962 wpw1 := wpru1.Watch()
963 defer testing.AssertStop(c, wpw1)
964 s.assertChange(c, wpw1, expectChanged, nil)
965 s.assertNoChange(c, wpw1)
966
967 // Join the first requirer unit, and check the provider units see it.
968 err = wpru0.EnterScope(nil)
969 c.Assert(err, gc.IsNil)
970 wps0, err := wpru0.Settings()
971 c.Assert(err, gc.IsNil)
972 expectChanged = map[string]map[string]interface{}{
973 "wordpress/0": wps0.Map(),
974 }
975 s.assertChange(c, msw0, expectChanged, nil)
976 s.assertNoChange(c, msw0)
977 s.assertChange(c, msw1, expectChanged, nil)
978 s.assertNoChange(c, msw1)
979
980 // Join again, check no-op.
981 err = wpru0.EnterScope(nil)
982 c.Assert(err, gc.IsNil)
983 s.assertNoChange(c, msw0)
984 s.assertNoChange(c, msw1)
985
986 // Join the second requirer, and check the provider units see the change.
987 err = wpru1.EnterScope(nil)
988 c.Assert(err, gc.IsNil)
989 wps1, err := wpru1.Settings()
990 c.Assert(err, gc.IsNil)
991 expectChanged = map[string]map[string]interface{}{
992 "wordpress/1": wps1.Map(),
993 }
994 s.assertChange(c, msw0, expectChanged, nil)
995 s.assertNoChange(c, msw0)
996 s.assertChange(c, msw1, expectChanged, nil)
997 s.assertNoChange(c, msw1)
998
999 // Verify that neither requirer has observed any change to the relation.
1000 s.assertNoChange(c, wpw0)
1001 s.assertNoChange(c, wpw1)
1002
1003 // Change settings for the first requirer, check providers see it...
1004 wps0 = changeSettings(c, wpru0)
1005 expectChanged = map[string]map[string]interface{}{
1006 "wordpress/0": wps0.Map(),
1007 }
1008 s.assertChange(c, msw0, expectChanged, nil)
1009 s.assertNoChange(c, msw0)
1010 s.assertChange(c, msw1, expectChanged, nil)
1011 s.assertNoChange(c, msw1)
1012
1013 // ...and requirers don't.
1014 s.assertNoChange(c, wpw0)
1015 s.assertNoChange(c, wpw1)
1016
1017 // Depart the second requirer and check the providers see it...
1018 err = wpru1.LeaveScope()
1019 c.Assert(err, gc.IsNil)
1020 expectDeparted := []string{"wordpress/1"}
1021 s.assertChange(c, msw0, nil, expectDeparted)
1022 s.assertNoChange(c, msw0)
1023 s.assertChange(c, msw1, nil, expectDeparted)
1024 s.assertNoChange(c, msw1)
1025
1026 // ...and the requirers don't.
1027 s.assertNoChange(c, wpw0)
1028 s.assertNoChange(c, wpw1)
1029
1030 // Cleanup handled by defers as before.
1031}
1032
1033func (s *WatchScopeSuite) TestProviderRequirerContainer(c *gc.C) {
1034 // Create a pair of services and a relation between them.
1035 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql"))
1036 c.Assert(err, gc.IsNil)
1037 mysqlEP, err := mysql.Endpoint("juju-info")
1038 c.Assert(err, gc.IsNil)
1039 logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logging"))
1040 c.Assert(err, gc.IsNil)
1041 loggingEP, err := logging.Endpoint("info")
1042 c.Assert(err, gc.IsNil)
1043 rel, err := s.State.AddRelation(mysqlEP, loggingEP)
1044 c.Assert(err, gc.IsNil)
1045
1046 // Change mysqlEP to match the endpoint that will actually be used by the relation.
1047 mysqlEP.Scope = charm.ScopeContainer
1048
1049 // Add some units to the services and set their private addresses.
1050 addUnits := func(i int) (*state.RelationUnit, *state.RelationUnit) {
1051 msu, err := mysql.AddUnit()
1052 c.Assert(err, gc.IsNil)
1053 msru, err := rel.Unit(msu)
1054 c.Assert(err, gc.IsNil)
1055 c.Assert(msru.Endpoint(), gc.Equals, mysqlEP)
1056 err = msru.EnterScope(nil)
1057 c.Assert(err, gc.IsNil)
1058 err = msru.LeaveScope()
1059 c.Assert(err, gc.IsNil)
1060 lgu, err := s.State.Unit("logging/" + strconv.Itoa(i))
1061 c.Assert(err, gc.IsNil)
1062 lgru, err := rel.Unit(lgu)
1063 c.Assert(err, gc.IsNil)
1064 c.Assert(lgru.Endpoint(), gc.Equals, loggingEP)
1065 return msru, lgru
1066 }
1067 msru0, lgru0 := addUnits(0)
1068 msru1, lgru1 := addUnits(1)
1069
1070 // ---------- Single role active ----------
1071
1072 // Start watching the relation from the perspective of the first unit, and
1073 // check the initial event.
1074 msw0 := msru0.Watch()
1075 defer testing.AssertStop(c, msw0)
1076 s.assertChange(c, msw0, nil, nil)
1077 s.assertNoChange(c, msw0)
1078
1079 // Join the unit to the relation, change its settings, and check that
1080 // nothing apparently happens.
1081 err = msru0.EnterScope(nil)
1082 c.Assert(err, gc.IsNil)
1083 mss0 := changeSettings(c, msru0)
1084 s.assertNoChange(c, msw0)
1085
1086 // Watch the relation from the perspective of the second provider, and
1087 // check initial event.
1088 msw1 := msru1.Watch()
1089 defer testing.AssertStop(c, msw1)
1090 s.assertChange(c, msw1, nil, nil)
1091 s.assertNoChange(c, msw1)
1092
1093 // Join the second provider unit to the relation, and check that neither
1094 // watching unit observes any change.
1095 err = msru1.EnterScope(nil)
1096 c.Assert(err, gc.IsNil)
1097 s.assertNoChange(c, msw1)
1098 s.assertNoChange(c, msw0)
1099
1100 // Change the unit's settings, and check that nothing apparently happens.
1101 mss1 := changeSettings(c, msru1)
1102 s.assertNoChange(c, msw1)
1103 s.assertNoChange(c, msw0)
1104
1105 // ---------- Two roles active ----------
1106
1107 // Start a watch from the first requirer unit's perspective, and check it
1108 // only sees the first provider (with which it shares a container).
1109 lgw0 := lgru0.Watch()
1110 defer testing.AssertStop(c, lgw0)
1111 expectChanged := map[string]map[string]interface{}{
1112 "mysql/0": mss0.Map(),
1113 }
1114 s.assertChange(c, lgw0, expectChanged, nil)
1115 s.assertNoChange(c, lgw0)
1116
1117 // Join the first requirer unit, and check that only the first provider
1118 // observes the change.
1119 err = lgru0.EnterScope(nil)
1120 c.Assert(err, gc.IsNil)
1121 lgs0, err := lgru0.Settings()
1122 c.Assert(err, gc.IsNil)
1123 expectChanged = map[string]map[string]interface{}{
1124 "logging/0": lgs0.Map(),
1125 }
1126 s.assertChange(c, msw0, expectChanged, nil)
1127 s.assertNoChange(c, msw0)
1128 s.assertNoChange(c, msw1)
1129 s.assertNoChange(c, lgw0)
1130
1131 // Watch from the second requirer's perspective, and check it only sees the
1132 // second provider.
1133 lgw1 := lgru1.Watch()
1134 defer testing.AssertStop(c, lgw1)
1135 expectChanged = map[string]map[string]interface{}{
1136 "mysql/1": mss1.Map(),
1137 }
1138 s.assertChange(c, lgw1, expectChanged, nil)
1139 s.assertNoChange(c, lgw1)
1140
1141 // Join the second requirer, and check that the first provider observes it...
1142 err = lgru1.EnterScope(nil)
1143 c.Assert(err, gc.IsNil)
1144 lgs1, err := lgru1.Settings()
1145 c.Assert(err, gc.IsNil)
1146 expectChanged = map[string]map[string]interface{}{
1147 "logging/1": lgs1.Map(),
1148 }
1149 s.assertChange(c, msw1, expectChanged, nil)
1150 s.assertNoChange(c, msw1)
1151
1152 // ...and that nothing else sees anything.
1153 s.assertNoChange(c, msw0)
1154 s.assertNoChange(c, lgw0)
1155 s.assertNoChange(c, lgw1)
1156
1157 // Change the second provider's settings and check that the second
1158 // requirer notices...
1159 mss1 = changeSettings(c, msru1)
1160 expectChanged = map[string]map[string]interface{}{
1161 "mysql/1": mss1.Map(),
1162 }
1163 s.assertChange(c, lgw1, expectChanged, nil)
1164 s.assertNoChange(c, lgw1)
1165
1166 // ...but that nothing else does.
1167 s.assertNoChange(c, msw0)
1168 s.assertNoChange(c, msw1)
1169 s.assertNoChange(c, msw0)
1170
1171 // Finally, depart the first provider, and check that only the first
1172 // requirer observes any change.
1173 err = msru0.LeaveScope()
1174 c.Assert(err, gc.IsNil)
1175 expectDeparted := []string{"mysql/0"}
1176 s.assertChange(c, lgw0, nil, expectDeparted)
1177 s.assertNoChange(c, lgw0)
1178 s.assertNoChange(c, lgw1)
1179 s.assertNoChange(c, msw0)
1180 s.assertNoChange(c, msw1)
1181
1182 // Again, I think we're done, and can be comfortable that the appropriate
1183 // connections are in place.
1184}
1185
1186func changeSettings(c *gc.C, ru *state.RelationUnit) *state.Settings {
1187 node, err := ru.Settings()
1188 c.Assert(err, gc.IsNil)
1189 value, _ := node.Get("value")
1190 v, _ := value.(int)
1191 node.Set("value", v+1)
1192 _, err = node.Write()
1193 c.Assert(err, gc.IsNil)
1194 return node
1195}
1196
1197func (s *WatchScopeSuite) assertChange(
1198 c *gc.C, w *state.RelationUnitsWatcher,
1199 changed map[string]map[string]interface{},
1200 departed []string,
1201) {
1202 s.State.StartSync()
1203 select {
1204 case ch, ok := <-w.Changes():
1205 if !ok {
1206 c.Fatalf("channel closed; watcher error: %#v", w.Err())
1207 }
1208 c.Assert(ch.Changed, gc.HasLen, len(changed))
1209 for name, m := range changed {
1210 c.Assert(ch.Changed[name].Settings, gc.DeepEquals, m)
1211 }
1212 c.Assert(departed, jc.SameContents, ch.Departed)
1213 case <-time.After(5 * time.Second):
1214 c.Fatalf("expected changed %#v, departed %#v; got nothing", changed, departed)
1215 }
1216}
1217
1218func (s *WatchScopeSuite) assertNoChange(c *gc.C, w *state.RelationUnitsWatcher) {
1219 s.State.StartSync()
1220 select {
1221 case ch := <-w.Changes():
1222 c.Fatalf("got %#v, expected nothing", ch)
1223 case <-time.After(50 * time.Millisecond):
1224 }
1225}

Subscribers

People subscribed via source and target branches

to status/vote changes: