correctly set the team number for tests

Without this, it's going to pick some random team number on some
computers and fail on others (depending on the network configuration).

Change-Id: I182ed50299170de737812eadee68ff418e6518e6
diff --git a/aos/common/controls/control_loop_test.cc b/aos/common/controls/control_loop_test.cc
index 899b96b..40357b2 100644
--- a/aos/common/controls/control_loop_test.cc
+++ b/aos/common/controls/control_loop_test.cc
@@ -32,7 +32,7 @@
 
     new_state->enabled = enabled;
     new_state->autonomous = false;
-    new_state->team_id = 971;
+    new_state->team_id = team_id_;
 
     new_state.Send();
   }
diff --git a/aos/common/controls/control_loop_test.h b/aos/common/controls/control_loop_test.h
index 13c4f0d..a1165a9 100644
--- a/aos/common/controls/control_loop_test.h
+++ b/aos/common/controls/control_loop_test.h
@@ -19,6 +19,9 @@
   ControlLoopTest();
   virtual ~ControlLoopTest();
 
+  void set_team_id(uint16_t team_id) { team_id_ = team_id; }
+  uint16_t team_id() const { return team_id_; }
+
   // Sends out all of the required queue messages.
   void SendMessages(bool enabled);
   // Ticks time for a single control loop cycle.
@@ -37,6 +40,8 @@
   static constexpr ::aos::time::Time kDSPacketTime =
       ::aos::time::Time::InMS(20);
 
+  uint16_t team_id_ = 971;
+
   ::aos::time::Time last_ds_time_ = ::aos::time::Time::InSeconds(0);
   ::aos::time::Time current_time_ = ::aos::time::Time::InSeconds(0);
 
diff --git a/aos/common/network/team_number.cc b/aos/common/network/team_number.cc
index 38b169a..626d1e6 100644
--- a/aos/common/network/team_number.cc
+++ b/aos/common/network/team_number.cc
@@ -11,7 +11,7 @@
 namespace network {
 namespace {
 
-uint16_t override_team = 0;
+uint16_t override_team;
 
 uint16_t *DoGetTeamNumber() {
   if (override_team != 0) return &override_team;
diff --git a/aos/common/network/team_number.h b/aos/common/network/team_number.h
index 08103ed..520b9ba 100644
--- a/aos/common/network/team_number.h
+++ b/aos/common/network/team_number.h
@@ -15,6 +15,7 @@
 // before GetTeamNumber() is ever called.
 // Overriding to team 0 won't work.
 // Intended only for tests.
+// Guaranteed to be safe to call during static initialization time.
 void OverrideTeamNumber(uint16_t team);
 
 }  // namespace network
diff --git a/frc971/control_loops/claw/claw.gyp b/frc971/control_loops/claw/claw.gyp
index e8e3276..a3e556a 100644
--- a/frc971/control_loops/claw/claw.gyp
+++ b/frc971/control_loops/claw/claw.gyp
@@ -49,6 +49,7 @@
         '<(DEPTH)/frc971/control_loops/control_loops.gyp:state_feedback_loop',
         '<(AOS)/common/controls/controls.gyp:control_loop_test',
         '<(DEPTH)/frc971/control_loops/control_loops.gyp:position_sensor_sim',
+        '<(DEPTH)/frc971/control_loops/control_loops.gyp:team_number_test_environment',
       ],
     },
     {
diff --git a/frc971/control_loops/claw/claw_lib_test.cc b/frc971/control_loops/claw/claw_lib_test.cc
index 8d0ac78..ca610d9 100644
--- a/frc971/control_loops/claw/claw_lib_test.cc
+++ b/frc971/control_loops/claw/claw_lib_test.cc
@@ -9,6 +9,7 @@
 #include "frc971/control_loops/claw/claw.h"
 #include "frc971/control_loops/position_sensor_sim.h"
 #include "frc971/constants.h"
+#include "frc971/control_loops/team_number_test_environment.h"
 
 using ::aos::time::Time;
 
@@ -76,7 +77,9 @@
                     ".frc971.control_loops.claw_queue.output",
                     ".frc971.control_loops.claw_queue.status"),
         claw_(&claw_queue_),
-        claw_plant_() {}
+        claw_plant_() {
+    set_team_id(kTeamNumber);
+  }
 
   void VerifyNearGoal() {
     claw_queue_.goal.FetchLatest();
diff --git a/frc971/control_loops/control_loops.gyp b/frc971/control_loops/control_loops.gyp
index 6e882e1..2c0b4d5 100644
--- a/frc971/control_loops/control_loops.gyp
+++ b/frc971/control_loops/control_loops.gyp
@@ -1,6 +1,20 @@
 {
   'targets': [
     {
+      'target_name': 'team_number_test_environment',
+      'type': 'static_library',
+      'sources': [
+        'team_number_test_environment.cc'
+      ],
+      'dependencies': [
+        '<(AOS)/common/network/network.gyp:team_number',
+        '<(EXTERNALS):gtest',
+      ],
+      'export_dependent_settings': [
+        '<(EXTERNALS):gtest',
+      ],
+    },
+    {
       'target_name': 'state_feedback_loop_test',
       'type': 'executable',
       'sources': [
diff --git a/frc971/control_loops/fridge/fridge.gyp b/frc971/control_loops/fridge/fridge.gyp
index ade2d0a..1f3eebb 100644
--- a/frc971/control_loops/fridge/fridge.gyp
+++ b/frc971/control_loops/fridge/fridge.gyp
@@ -50,6 +50,7 @@
         '<(DEPTH)/frc971/control_loops/control_loops.gyp:state_feedback_loop',
         '<(AOS)/common/controls/controls.gyp:control_loop_test',
         '<(DEPTH)/frc971/control_loops/control_loops.gyp:position_sensor_sim',
+        '<(DEPTH)/frc971/control_loops/control_loops.gyp:team_number_test_environment',
       ],
     },
     {
diff --git a/frc971/control_loops/fridge/fridge_lib_test.cc b/frc971/control_loops/fridge/fridge_lib_test.cc
index 98deb8e..120bd2c 100644
--- a/frc971/control_loops/fridge/fridge_lib_test.cc
+++ b/frc971/control_loops/fridge/fridge_lib_test.cc
@@ -9,13 +9,13 @@
 #include "frc971/control_loops/fridge/fridge.q.h"
 #include "frc971/control_loops/fridge/fridge.h"
 #include "frc971/constants.h"
+#include "frc971/control_loops/team_number_test_environment.h"
 
 using ::aos::time::Time;
 
 namespace frc971 {
 namespace control_loops {
 namespace testing {
-
 // Class which simulates the fridge and sends out queue messages with the
 // position.
 class FridgeSimulation {
@@ -131,7 +131,9 @@
                       ".frc971.control_loops.fridge_queue.output",
                       ".frc971.control_loops.fridge_queue.status"),
         fridge_(&fridge_queue_),
-        fridge_plant_() {}
+        fridge_plant_() {
+    set_team_id(kTeamNumber);
+  }
 
   void VerifyNearGoal() {
     fridge_queue_.goal.FetchLatest();
diff --git a/frc971/control_loops/team_number_test_environment.cc b/frc971/control_loops/team_number_test_environment.cc
new file mode 100644
index 0000000..624a119
--- /dev/null
+++ b/frc971/control_loops/team_number_test_environment.cc
@@ -0,0 +1,15 @@
+#include "frc971/control_loops/team_number_test_environment.h"
+
+#include "aos/common/network/team_number.h"
+
+namespace frc971 {
+namespace control_loops {
+namespace testing {
+
+void TeamNumberEnvironment::SetUp() {
+  ::aos::network::OverrideTeamNumber(kTeamNumber);
+}
+
+}  // namespace testing
+}  // namespace control_loops
+}  // namespace frc971
diff --git a/frc971/control_loops/team_number_test_environment.h b/frc971/control_loops/team_number_test_environment.h
new file mode 100644
index 0000000..f0c1a16
--- /dev/null
+++ b/frc971/control_loops/team_number_test_environment.h
@@ -0,0 +1,30 @@
+#ifndef FRC971_CONTROL_LOOPS_TEAM_NUMBER_TEST_ENVIRONMENT_H_
+#define FRC971_CONTROL_LOOPS_TEAM_NUMBER_TEST_ENVIRONMENT_H_
+
+#include "gtest/gtest.h"
+
+namespace frc971 {
+namespace control_loops {
+namespace testing {
+
+// The team number we use for tests.
+static const int kTeamNumber = 1;
+
+// Overrides the team number to kTeamNumber before any test consructors run.
+// This is important for tests which retrieve constants values during
+// construction.
+class TeamNumberEnvironment : public ::testing::Environment {
+ public:
+  void SetUp() override;
+};
+
+// The static variable in a header is intentional. Kind of a hack, undefined
+// order, but that works OK here.
+static ::testing::Environment* const team_number_env =
+    ::testing::AddGlobalTestEnvironment(new TeamNumberEnvironment());
+
+}  // namespace testing
+}  // namespace control_loops
+}  // namespace frc971
+
+#endif  // FRC971_CONTROL_LOOPS_TEAM_NUMBER_TEST_ENVIRONMENT_H_