コード例 #1
0
        public void TestConstructor()
        {
            DateTime lastStartTime = DateTime.Parse("2017-08-04T17:52:13.0419502Z", null, DateTimeStyles.RoundtripKind);

            // null docker config
            Assert.Throws <ArgumentNullException>(() => new DockerRuntimeModule("mod1", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, null, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars));

            // bad desired status
            Assert.Throws <ArgumentOutOfRangeException>(() => new DockerRuntimeModule("mod1", "version1", (ModuleStatus)int.MaxValue, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars));

            // bad restart policy
            Assert.Throws <ArgumentOutOfRangeException>(() => new DockerRuntimeModule("mod1", "version1", ModuleStatus.Running, (RestartPolicy)int.MaxValue, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars));

            // bad runtime status
            Assert.Throws <ArgumentOutOfRangeException>(() => new DockerRuntimeModule("mod1", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, (ModuleStatus)int.MaxValue, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars));

            // bad restart count
            Assert.Throws <ArgumentOutOfRangeException>(() => new DockerRuntimeModule("mod1", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, -1, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars));

            // bad pull policy
            Assert.Throws <ArgumentOutOfRangeException>(() => new DockerRuntimeModule("mod1", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, (ImagePullPolicy)int.MaxValue, DefaultConfigurationInfo, EnvVars));

            var env1 = new DockerRuntimeModule("name", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, null, lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var env2 = new DockerRuntimeModule("name", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", DateTime.MinValue, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var env3 = new DockerRuntimeModule("name", "version1", ModuleStatus.Running, RestartPolicy.OnUnhealthy, Config1, 0, "Running 1 minute", lastStartTime, DateTime.MinValue, 0, DateTime.MinValue, ModuleStatus.Running, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);

            Assert.NotNull(env1);
            Assert.NotNull(env2);
            Assert.NotNull(env3);
        }
        public async Task <ModuleSet> GetModulesAsync(CancellationToken token)
        {
            IEnumerable <ModuleRuntimeInfo> moduleStatuses = await this.moduleStatusProvider.GetModules(token);

            var       modules   = new List <IModule>();
            ModuleSet moduleSet = this.deploymentConfig.GetModuleSet();

            foreach (ModuleRuntimeInfo moduleRuntimeInfo in moduleStatuses)
            {
                if (moduleRuntimeInfo.Type != "docker" || !(moduleRuntimeInfo is ModuleRuntimeInfo <DockerReportedConfig> dockerRuntimeInfo))
                {
                    Events.InvalidModuleType(moduleRuntimeInfo);
                    continue;
                }

                if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
                {
                    dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters(), Option.None <string>()), ImagePullPolicy.OnCreate, Core.Constants.HighestPriority, new ConfigurationInfo(), null, null);
                }

                Option <ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));

                string  image = !string.IsNullOrWhiteSpace(dockerRuntimeInfo.Config.Image) ? dockerRuntimeInfo.Config.Image : dockerModule.Config.Image;
                var     dockerReportedConfig = new DockerReportedConfig(image, dockerModule.Config.CreateOptions, dockerRuntimeInfo.Config.ImageHash, dockerModule.Config.Digest);
                IModule module;
                switch (moduleRuntimeInfo.Name)
                {
                case Core.Constants.EdgeHubModuleName:
                    module = new EdgeHubDockerRuntimeModule(
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue),
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeInfo.ModuleStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.Priority,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;

                case Core.Constants.EdgeAgentModuleName:
                    module = new EdgeAgentDockerRuntimeModule(
                        dockerReportedConfig,
                        moduleRuntimeInfo.ModuleStatus,
                        (int)dockerRuntimeInfo.ExitCode,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue),
                        dockerModule.ImagePullPolicy,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;

                default:
                    module = new DockerRuntimeModule(
                        moduleRuntimeInfo.Name,
                        dockerModule.Version,
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue),
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeInfo.ModuleStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.Priority,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env,
                        dockerModule.Secrets);
                    break;
                }

                modules.Add(module);
            }

            return(ModuleSet.Create(modules.ToArray()));
        }